【发布时间】:2010-01-13 16:45:36
【问题描述】:
我在基于 C# 的代码中使用实体框架。我遇到了意想不到的怪事,正在寻找建议。
案例 1、2、3、4...
项目:
RivWorks.dll
RivWorks.Service.dll
RivWorks.Alpha.dll
示例(所有这些工作):
RivWorks.Alpha.dll:
public static bool EndNegotitation(long ProductID)
{
var product = (from a in _dbFeed.AutoWithImage
where a.AutoID == ProductID select a).FirstOrDefault();
...
}
RivWorks.Service.dll
public static RivWorks.Model.NegotiationAutos.AutoWithImage
GetProductById(long productId)
{
var myProduct = from a in _dbFeed.AutoWithImage
where a.AutoID == productId select a;
return myProduct.FirstOrDefault();
}
public static List<RivWorks.Model.NegotiationAutos.AutoWithImage>
GetProductByCompany(Guid companyId)
{
var myProduct = from a in _dbFeed.AutoWithImage
where a.CompanyID == companyId select a;
return myProduct.ToList();
}
等
案例“怪异”:
RivWorks.Web.Service.dll(WCF 项目)
包含与其他项目相同的引用。
public NegotiateSetup GetSetup(string method, string jsonInput)
{
...
long.TryParse(ProductID, out result);
var product = (from a in _dbFeed.AutoWithImage
where a.AutoID == result select a).FirstOrDefault();
...
}
我收到此编译时错误(“where”一词在我的编辑器中突出显示):
无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型
有什么想法会导致这种情况吗?
【问题讨论】:
-
这听起来很奇怪。如果您删除对
FirstOrDefault的调用,会发生什么情况?显然它会在你之后尝试使用product时失败,但该语句是否编译? -
另外,如果你把它改成
var product = _dbFeed.AutoWithImage.Where(a => a.AutoID == result);会发生什么?让我们把查询表达式排除在外...... -
所有这些例子都失败了。但是,我检查了所有代码片段中的 Using 语句,发现我缺少一个:Using System.Linq;这修复了错误。
标签: c# .net entity-framework lambda