通过LINQ语句返回记录

时间:2013-03-11 13:10:53

标签: asp.net linq

DataContext db = new DataContext();
Select_Utilities SelectedUtility = (from su in db.Select_Utilities
                                    where su.id == SelectUtilityId
                                    && su.Worksite_Id == WorksiteId
                                     && su.Utility_Company.id == UtilityCompanyId
                                     select su).FirstOrDefault();

然后我想说SelectedUtility.comment =“无论评论是什么”;

但是获得错误:不能将'Select_Utility'类型转换为'Select_Utilities'

在声明中使用'FirstOrDefault'任何建议吗?

由于

2 个答案:

答案 0 :(得分:3)

将结果类型更改为Select_Utility

Select_Utility SelectedUtility =  // here
   (from su in db.Select_Utilities
    where su.id == SelectUtilityId && 
          su.Worksite_Id == WorksiteId && 
          su.Utility_Company.id == UtilityCompanyId
    select su).FirstOrDefault();

答案 1 :(得分:1)

我猜Select_Utilities是您的Entity表的名称,它返回Select_Utility个对象的集合。试试:

Select_Utility selectedUtility = (from su in db.Select_Utilities
                                  where su.id == SelectUtilityId
                                     && su.Worksite_Id == WorksiteId
                                     && su.Utility_Company.id == UtilityCompanyId
                                  select su).FirstOrDefault();