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 がこれに答えていないなんて信じられない。

4

2 に答える 2

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();
于 2012-11-06T12:44:14.780 に答える
0

あなたはこれを行うことができます:

List<int> AuctionIds = _auctionContext.Auctions
    .Where(a => a.AuctionEventId == auction.AuctionEventId)
    .Select(a => a.Id)
    .ToList();
于 2012-11-06T13:20:48.280 に答える