Linq to SQL - 填充IEnumerable属性

时间:2011-10-12 18:34:05

标签: linq-to-sql collections ienumerable

我试图从表格中提取一些记录(BidNames)并将它们传递给视图模型中的视图。我的视图模型只包含一组BidName记录。

我的观点模型:

public class BidNamesVM
{
    public IEnumerable<BidName> BidNames { get; set; }
}

我在填充BidNames集合时遇到问题。从bn到BidNames的转换无效。

from bn in BidNames
where bn.CustomerID == 1160 && bn.Customer.Bids.Any(b=>b.Item.AuctionID == 2)
select new BidNamesVM
{
    BidNames = bn 
}

在查询中填写BidNames需要做什么?

非常感谢,

BK

1 个答案:

答案 0 :(得分:1)

您的LINQ查询已经返回IEnumerable<BidName>,其中bn表示BidName的单个实例。试试这个:

BidNamesVM bnVM = new BidNamesVM();
bnVM.BidNames = from bn in BidNames
                where bn.CustomerID == 1160 && bn.Customer.Bids.Any(b=>b.Item.AuctionID == 2)
                select bn;

在您的示例中,您尝试将BidName的实例设置为IEnumerable<BidName>类型的属性,但由于显而易见的原因,该属性无效。