I am trying to pull some records from a table (BidNames) and pass them to a view in a view model. My view model consists of nothing more than a collection of BidName records.
My view model:
public class BidNamesVM
{
public IEnumerable<BidName> BidNames { get; set; }
}
I'm having problems populating the BidNames collection. The conversion from bn to BidNames isn't working.
from bn in BidNames
where bn.CustomerID == 1160 && bn.Customer.Bids.Any(b=>b.Item.AuctionID == 2)
select new BidNamesVM
{
BidNames = bn
}
What do I have to do to populate BidNames in my query?开发者_开发问答
Many thanks,
BK
Your LINQ query already returns an IEnumerable<BidName>
, with bn
representing an individual instance of BidName
. Try this:
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;
In your example, you were trying to set an instance of BidName
to a property of type IEnumerable<BidName>
, which won't work for obvious reasons.
精彩评论