好日子,System.Linq.Select方法错误:LINQ到实体无法识别方法
我的代码生成运行时错误,我不知道如何解决这个问题。任何帮助,将不胜感激。
我们的代码是经过“报价”列表调用下面的第二个转换方法。报价是来自EDMX模型的一个对象。第一个Convert方法接受对象并将其转换为DTO类,然后返回。
运行程序时,我遇到以下错误:
LINQ to Entities does not recognize the method '...Domain.Holdings.OfferDto Convert(...Repository.Offer, System.Nullable`1[System.Int32])' method, and this method cannot be translated into a store expression.
的代码如下:
public class OfferDtoMapping
{
public static OfferDto Convert(Offer offer, int? participantId)
{
if (offer == null)
throw new ArgumentException("The Offer object supplied for conversion cannot be null");
var unitCost = offer.UnitCost;
if (offer.Trust.Company.AllowInterDiv && participantId.HasValue)
{
Assign another value to unitCost...
}
var output = new OfferDto
{
Assign the offer properties to the OfferDto properties...
};
return output;
}
public static IList<OfferDto> Convert(IQueryable<Offer> offerList)
{
return offerList == null ? new List<OfferDto>() : offerList.Select(ol => Convert(ol, null)).ToList();
}
}
在此行中出现的错误:
return offerList == null ? new List<OfferDto>() : offerList.Select(ol => Convert(ol, null)).ToList();
我怀疑这个错误与我通过采用2个inp的方法投影offerList的事实有关ut参数。我们尝试传递0而不是NULL(因为我们最初认为这是问题),但仍然收到类似的错误消息。
为了保持整个我们的解决方案的一致性,我们要使用选择方法,而不是用foreach语句代替这种方式来保持。
在我们目前的情况下有没有使用Select方法的方法?
预先感谢您。
非常好,谢谢你,谢谢大家这样快速的回答。我测试了它,它工作。 – Matei 2012-03-06 12:07:56
作为'ToList()'的替代方法,可以使用'AsEnumerable()',这可能更有效,因为它不需要分配两次列表。 – svick 2012-03-06 12:34:08