开发者

Using LINQ to get all the records in a table having the same value in a specific column

I am trying to get all the rows in a table having the same value in a column. I got it working by using group by:

var groupedData = from row in Tab1Model.ExcelGridDataSource.AsEnumerable()
                                            group row by row.Field<string>("A");


        foreach (var group in groupedData)
        {
            if (group.Count() > 1)
            {
                                    //select from each group only the DataRows  
                                    //having a certain value in a second column
      开发者_运维问答          foreach (var dataRow in group)
                {
                    multipleRowsList.Add(dataRow);
                }
            }
        }

I would like to avoid calling foreach ,get only the groups having a count > 1 and then get ONLY the DataRows that have a second column with a specific value. Thanks!


try this:

var query = from row in excelDataSource 
            group row by row.Field<string>("A") into g 
            select new { Value = g.Key, Rows = g };
var nonZeroRows= from q in query 
                       where q.Rows.Count() > 0 
                       select q.Rows;
// at this point you have an enumerable of enumerables of tablerows.
var list = nonZeroRows.Aggregate(Enumerable.Empty<TableRow>(), 
           (a, b) => a.Concat(b.Where(c => c.Something == true)); // your condition here


Thanks Atanamir! Here is the final code, just wonder if you have any better ways of doing it. the end goal of this is to flag one of the rows that is entered twice.

var groupedData = from row in Tab1Model.ExcelGridDataSource.AsEnumerable()
                          group row by row.Field<string>("A")
                          into g
                          select new {Value = g.Key, Rows = g};
        var nonZeroesRows = from q in groupedData
                            where q.Rows.Count() > 1
                            select q.Rows;
        //at this point you have an enumerable of enumerables of tables rows
        var listRows = nonZeroesRows.Aggregate(Enumerable.Empty<DataRow>(),
                                               (a, b) => a.Concat(b.Where(c => c.Field<bool>("Omit Row") == false)));

        //grouped them again and get only the last row from the group wiht a count > 1 
        var doubleRows = from row in listRows
                         group row by row.Field<string>("A")
                         into g
                         where g.Count() > 1
                         select g.Last();


Or maybe better:

            var groupedData = from row in Tab1Model.ExcelGridDataSource.AsEnumerable()
                          group row by row.Field<string>("A")
                          into g
                                            where g.Count() > 1
                          select new {/*Value = g.Key,*/ Rows = g};

        //at this point you have an enumerable of enumerables of tables rows
        var listRows = groupedData.Aggregate(Enumerable.Empty<DataRow>(),
                                               (a, b) => a.Concat(b.Rows.Where(c => c.Field<bool>("Omit Row") == false)));

        //grouped them again and get only the last row from the group wiht a count > 1 
        var doubleRows = from row in listRows
                         group row by row.Field<string>("A")
                         into g
                         where g.Count() > 1
                         select g.Last();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜