2012-11-06 15 views
4

我有这个疑问我的清单应包含哪些类型?

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

但是我收到一个编译错误

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

应该AuctionIds是什么类型的?

编辑

的AuctionIds场其实是在另一个类(模型类),所以我不能只用VAR。我不能相信Jon Skeet没有回答这个问题。

回答

0

你加入一个匿名对象到List<int> .. 如果你要做到这一点,你有它的方式。我会用var关键字..

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

原因是我不知道什么类型的匿名对象是......但编译器应该能够处理它。

编辑:

嗯,有关创建AuctionIDModel类?

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

选择a.Id不起作用。 –

+0

我不想使用var,因为我实际上有另一个类中的AuctionIds字段。 –

+0

mmn ..我能想到的唯一方法是创建一个模型来存储您的数据。然后将该Model对象添加到列表中。 –

0

你可以这样做:因为我使用的OData和EF

List<int> AuctionIds = _auctionContext.Auctions 
    .Where(a => a.AuctionEventId == auction.AuctionEventId) 
    .Select(a => a.Id) 
    .ToList(); 
相关问题