我在我的C#代码中使用Entity Framework。我遇到了一个意外的怪事,并正在寻求建议。
情况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 表达式转换为类型“string”,因为它不是委托类型
有任何想法是什么导致了这个错误吗?
FirstOrDefault
的调用,会发生什么?显然,在之后尝试使用product
时它会失败,但是那个语句会编译通过吗? - Jon Skeetvar product = _dbFeed.AutoWithImage.Where(a => a.AutoID == result);
,那么会发生什么呢?让我们把查询表达式排除在外... - Jon Skeet