0

I am trying to make a dynamic grouping for LINQ and I am just not getting the syntax correct. Any help would be greatly appreciated. Here is my static LINQ code;

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

thanks

edit:
I did see some examples but it seems I am not smart enough to figure it. Also I just realized that I never stated myCollection is an ObservableCollection This is what I have tried to make the above query into;

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,
} );

This returns an error of;

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.
Xaphann
  • 3,195
  • 10
  • 42
  • 70

0 Answers0