关于Linq的group by的问题

iceMung 2013-03-20 05:03:45
from x in source.Rows.Cast<DataRow>()
group x by new { *** } into g
select new { Items = g };

group by 后面的***部分内容是不确定的,可能有多项,要如何写。
...全文
296 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanghuawei 2013-03-21
  • 打赏
  • 举报
回复
引用 7 楼 iceMung 的回复:
引用 3 楼 tanghuawei 的回复:C# code?12345678910111213141516171819202122232425262728293031323334353637class EnumerableDataRowList<T> : IEnumerable<T>, IEnumerable { IEnumerable dataRow……
怎么不行?你可以这样调用 group x by new { groupingFunction }
devmiao 2013-03-20
  • 打赏
  • 举报
回复
引用 8 楼 iceMung 的回复:
引用 6 楼 devmiao 的回复:new { a = xxx, b = xxx ... } 项数是不定的
那需要自己写一个集合类,并且实现 ICompareable 接口,传给 GroupBy。
iceMung 2013-03-20
  • 打赏
  • 举报
回复
项的数目是根据一个List的长度来确定的,而List的长度是不定的
iceMung 2013-03-20
  • 打赏
  • 举报
回复
引用 6 楼 devmiao 的回复:
new { a = xxx, b = xxx ... }
项数是不定的
iceMung 2013-03-20
  • 打赏
  • 举报
回复
引用 3 楼 tanghuawei 的回复:
C# code?12345678910111213141516171819202122232425262728293031323334353637class EnumerableDataRowList<T> : IEnumerable<T>, IEnumerable { IEnumerable dataRows; internal Enu……
好像不行~
devmiao 2013-03-20
  • 打赏
  • 举报
回复
new { a = xxx, b = xxx ... }
iceMung 2013-03-20
  • 打赏
  • 举报
回复
引用 2 楼 dr592112441 的回复:
from x in source.Rows.Cast<DataRow>() group x by new { x.columnName1,x.columnName2 } into g select new { Items = g };
不确定项以及项数...
iceMung 2013-03-20
  • 打赏
  • 举报
回复
引用 1 楼 yyantifa 的回复:
C# code?123Filed1=x.aaa,Filed2=x.bbb,Filed3=x.ccc
不确定项以及项数...
tanghuawei 2013-03-20
  • 打赏
  • 举报
回复

class EnumerableDataRowList<T> : IEnumerable<T>, IEnumerable
    {
        IEnumerable dataRows;
        internal EnumerableDataRowList(IEnumerable items)
        {
            dataRows = items;
        }
        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            foreach (T dataRow in dataRows)
                yield return dataRow;
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            IEnumerable<T> iEnumerable = this;
            return iEnumerable.GetEnumerator();
        }
    }

static StringBuilder stringBuilder = new StringBuilder();
        public static String GroupData(DataRow dataRow)
        {
            //这里的列你可以自己动态指定
            String[] columnNames = new[] { "Column1", "Column2"... };

            stringBuilder.Remove(0, stringBuilder.Length);
            foreach (String column in columnNames)
            {
                stringBuilder.Append(dataRow[column].ToString());
            }
            return stringBuilder.ToString();
        }

EnumerableDataRowList<DataRow> enumerableRowCollection = new EnumerableDataRowList<DataRow>(source.Rows);

            Func<DataRow, String> groupingFunction = GroupData;
var groupedDataRow = enumerableRowCollection.GroupBy(groupingFunction);
试试看
Mirror然 2013-03-20
  • 打赏
  • 举报
回复
from x in source.Rows.Cast<DataRow>() group x by new { x.columnName1,x.columnName2 } into g select new { Items = g };
yyantifa 2013-03-20
  • 打赏
  • 举报
回复
Filed1=x.aaa,
Filed2=x.bbb,
Filed3=x.ccc

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧