2017-04-30 25 views
0

我有一個方法,列表不能轉換爲ienumerable。我如何施放?system.collections.generic.list到system.collections.generic.ienumerable

public ActionResult Attending() 
{ 
    var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; 
    var gigs = _context.Attendances.Include(a => a.Gig.Artist).Include(a => a.Gig.Genre).Where(a => a.AttendeeId == userId).ToList(); 
    var viewModel = new GigsViewModel() 
    { 
     UpcomingGigs = gigs, 
     ShowActions = User.Identity.IsAuthenticated, 
     Heading = "Gigs I'm Attending" 
     }; 
     return View("Gigs", viewModel); 
    } 

這裏是我的ViewModel:

public class GigsViewModel 
{ 
    public IEnumerable<Gig> UpcomingGigs { get; set; } 
    public bool ShowActions { get; set; } 
    public string Heading { get; set; } 
} 

回答

0

您也可以先獲取所有與會者,並選擇演出。

IEnumerable<Gig> gigs = _context.Attendances.Where(a => a.AttendeeId == userId).Select(a => a.Gig).ToList(); 

要知道,如果可以有多個類似的演出,那麼也許你用鮮明只需要不同的一套演出的:

IEnumerable<Gig> gigs = _context.Attendances.Where(a => a.AttendeeId == userId).Select(a => a.Gig).Distinct().ToList(); 

多見於:https://msdn.microsoft.com/en-us/library/bb534803(v=vs.110).aspx

編輯:編輯老建議,差異和選擇添加。

+0

我試過了,現在它顯示不能IQueryable的隱式轉換爲IEnumerable的 –

0

嘗試使用下面的代碼,AsEnumerable()會做你的工作

var gigs = _context.Attendances.Where(a => a.AttendeeId == userId).select(a=> 
new Gig() 
{ 
    property = a.property //assuming property, you can repeat the same for other properties of Gig 
}).AsEnumerable(); 

一個LINQ數據庫查詢的結果是通常的IQueryable這是自IEnumerable,IQueryable的,和IEnumerable的。

在你的情況,它是linq數據庫查詢,所以你需要調用AsEnumerable();

+0

不能的IEnumerable 隱式轉換爲IEnumerable的的顯式轉換存在 –

+0

@ SaeefAl - 迪恩,你需要執行投影使用select作爲源類型(考勤)和目標類型(Gig)是不同的。請參閱最新的答案 –

相關問題