我有一个方法,其中list不能转换为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; }
}发布于 2017-04-30 21:50:42
您也可以先获取所有与会者,然后选择演出。
IEnumerable<Gig> gigs = _context.Attendances.Where(a => a.AttendeeId == userId).Select(a => a.Gig).ToList();请注意,如果可能有多个相似的gig,那么您可能只需要使用distinct的一组不同的gig:
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
已编辑:编辑旧建议、不同建议和已添加的选择。
发布于 2017-04-30 21:53:18
尝试使用下面的代码,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数据库查询的结果通常是从IEnumerable、IQueryable和IEnumerable派生的IQueryable。
在本例中,它是linq数据库查询,因此需要调用AsEnumerable();
https://stackoverflow.com/questions/43707075
复制相似问题