【问题标题】:c# group by multiple columns then select all fields where count > 1c# 按多列分组,然后选择 count > 1 的所有字段
【发布时间】:2018-11-15 04:26:49
【问题描述】:

我有一个这样的 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();

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

【问题讨论】:

  • .GroupBy(x =&gt; new { x["ReferenceID"], x["EventID"] }).Where(z =&gt; a.Count() &gt; 1)
  • 没有让该组获得债务人类型和遗留类型的其他值及其在一个组中我不想在一组字段中。我希望它在它之外谢谢

标签: c# linq c#-4.0 lambda


【解决方案1】:

通过做找到解决方案

  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()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2021-09-06
    • 1970-01-01
    • 2021-11-28
    • 2015-03-17
    • 2022-11-20
    • 2014-06-23
    相关资源
    最近更新 更多