BKahuna
BKahuna

Reputation: 611

Linq to SQL - Populating an IEnumerable property

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

Upvotes: 1

Views: 308

Answers (1)

Kyle Trauberman
Kyle Trauberman

Reputation: 25684

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.

Upvotes: 1

Related Questions