2012-11-06 2 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

당신은

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

이유는 .. 내가 var 키워드를 사용합니다 .. .. List<int>에 익명의 객체를 추가하는 난 몰라 익명 객체가 어떤 타입인지 알지만 컴파일러는 그것을 처리 할 수 ​​있어야합니다.

편집 :

어하는 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

실제로 다른 클래스에 AuctionIds 필드가 있기 때문에 var를 사용하고 싶지 않습니다. –

+0

mmn .. 내가 생각할 수있는 유일한 다른 방법은 데이터를 저장하기위한 모델을 만드는 것입니다. 그리고 Model 객체를 목록에 추가하십시오. –

0

이 작업을 수행 할 수 있습니다 내가 하나로, OData 및 EF를 사용하고 있기 때문에

List<int> AuctionIds = _auctionContext.Auctions 
    .Where(a => a.AuctionEventId == auction.AuctionEventId) 
    .Select(a => a.Id) 
    .ToList(); 
관련 문제