c#按多列分组,然后选择count> 1的所有字段

时间:2018-11-15 04:26:50

标签: c# linq c#-4.0 lambda

我有一个这样的lambda查询,

        List<string> ticketStatusOrder = new List<string>() { "Attended", "Issued", "Unpaid", "Cancelled" };

        var duplicate = dt.AsEnumerable()
            .OrderBy(x => ticketStatusOrder.IndexOf(x["TicketStatus"].ToString()))
            .GroupBy(x => new {EventID = x["EventID"].ToString(), ContactID = x["ContactID"].ToString()})
            .Select(x =>
            {
                var first = x.First();
                //return new {first.ItemArray};
                return new
                {
                    Type = first["type"],
                    Name = first["name"],
                    EventID = first["EventID"],
                    ContactID = first["ContactID"],
                    TicketStatus = first["TicketStatus"]
                };
            }).ToDataTable();

它没有按数字返回正确的顺序,有帮助吗?谢谢

1 个答案:

答案 0 :(得分:0)

边做边找到解决方案

  var duplicate = dt.AsEnumerable()
            .GroupBy(x => new {EventID = x["EventID"].ToString(), ContactID = x["ContactID"].ToString()})
            .Where(x => x.Count() == 1)
            .Select(x =>
            {
                var first = x.First();
                return new
                {
                    Type = first["type"],
                    Name = first["name"],
                    EventID = first["EventID"],
                    ContactID = first["ContactID"]
                };
            }).ToDataTable()

另一个问题,我们可以返回新的first.ItemArray这样的东西来返回first中的内容吗?谢谢