简体   繁体   中英

c# group by multiple columns then select all fields where count > 1

I have a lambda query like this,

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

Its not returning the correct order by number, anyhelp? thanks

found the solution by doing

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

Another question,can we return new first.ItemArray something like that to return whatever is in first? thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM