LINQ群由动态

时间:2014-12-16 18:19:58

标签: c# linq dynamic group-by dynamic-linq

我正在尝试为LINQ进行动态分组,我只是没有得到正确的语法。任何帮助将不胜感激。这是我的静态LINQ代码;

var query =
    from x in myCollection
    group x by x.Field1 into g
    select new myClass
    {
            /// <summary>
            /// Group logic removed
            /// </summary>
    };

感谢

编辑:
我确实看到了一些例子,但似乎我不够聪明。我也意识到我从未说过myCollection是一个ObservableCollection 这就是我试图将上述查询转化为;

var group = myCollection.GroupBy(
"new(it[\"Field1\"] as Field1,it[\"Field2\"]as Field2)", "it"
)
.Select(x =>
new myClass {
    Net = x.Sum(y => y.Net),
    Field1 = x.First().Field1,
    Field2 = x.First().Field2,
} );

这会返回错误;

The type arguments for method 'System.Linq.Enumerable.GroupBy<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>, System.Collections.Generic.IEqualityComparer<TKey>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

0 个答案:

没有答案