根据条件对结果进行分组

时间:2018-04-29 20:22:14

标签: c# asp.net-mvc

我正在尝试按“ SSN ”对结果进行分组。结果将循环显示在视图中。我对此非常陌生,不确定解决这个问题的正确方法是什么。我不确定我应该在这里显示多少代码。如果您想查看更多代码,请告诉我们。如果有人可以查看错误并提供一些帮助,请。

错误消息:

  

严重级代码描述项目文件行错误CS0029不能   隐式转换类型   'System.Collections.Generic.List>'至   'System.Collections.Generic.List'CardDistroE:\ wwwroot \ CardDistro \ CardDistro \ Controllers \ CardsController.cs 119

CardController.cs

setTimeout

CardViewModel.cs

    List<Card> QueriedBatchList;
    DBEntities db = new DBEntities();
    QueriedBatchList = db.Cards.Where(x => x.BatchID == null).GroupBy(x => x.SSN).ToList();

    CardViewModel CardViewModel = new CardViewModel();
    List<CardViewModel> CardDataList = QueriedBatchList.Select(x => new CardViewModel
    {
        CardID = x.CardID,
        SSN = x.SSN,
        PortID = x.PortID,
        UserID= x.UserID,
        Created= x.Created,
    }).ToList();

1 个答案:

答案 0 :(得分:2)

您应该在分组后应用Select。您应该选择Card实体集合。例如:

QueriedBatchList = db.Cards
    .Where(x => x.BatchID == null)
    .GroupBy(x => x.SSN)
    .Select(x => x.FirstOrDefault())
    .ToList();

此查询会按CardsSSN进行分组,然后选择每个组的第一条记录。

相关问题