What type should my list contain?

I have this request

List<int> AuctionIds = (from a in _auctionContext.Auctions where a.AuctionEventId == auction.AuctionEventId select new { a.Id }).ToList(); 

But I get a compilation error

 Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<int>' 

What type should be AuctionIds?

EDIT

The AuctionIds field is actually in another class (model class), so I cannot just use var. I cannot believe that John Skeet did not answer this.

+4
source share
2 answers

You add an anonymous object to the List<int> . If you did it the way you did it. I would use the var keyword.

 var AuctionIds = (from a in _auctionContext.Auctions where a.AuctionEventId == auction.AuctionEventId select new{Id = a.Id}).ToList(); 

The reason is because I don’t know what type of anonymous object is ... but the compiler must be able to handle it.

EDIT:

Eh, about creating an AuctionIDModel class?

  public class AuctionIDModel { int Id{get;set;} } List<AuctionIDModel> AuctionIds = (from a in _auctionContext.Auctions where a.AuctionEventId == auction.AuctionEventId select new AuctionIDModel{Id = a.Id}).ToList(); 
0
source

You can do it:

 List<int> AuctionIds = _auctionContext.Auctions .Where(a => a.AuctionEventId == auction.AuctionEventId) .Select(a => a.Id) .ToList(); 
0
source

Source: https://habr.com/ru/post/1444349/


All Articles