开发者

Linq to SQL - Populating an IEnumerable property

开发者 https://www.devze.com 2023-04-12 09:10 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号