Click here to Skip to main content
15,355,659 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First I had an error that said I should use a C# language version 7.2 or greater and fixed that. Then I got the error: "The type of one of the expressions in the group by clause is incorrect. Type inference failed in the call to 'GroupBy'"

What I have tried:

public object GetGenelStok(NetSatisContext context , string stokKodu)
var result = (from c in context.StokHareketleri.Where(c => c.StokKodu == stokKodu)
              group c by new(c.Hareket) --> Error line
              into g
              select new
                  Bilgi = g.Key.Hareket,
                  KayitSayisi = g.Count(),


            return result;
Updated 1-Feb-22 10:38am

1 solution

That appears to be an attempt to use a C# 9 feature - a "target-typed new expression".

But since there's no target available, the compiler will not let you use it.

You either need to specify the wrapper class type:
group c by new SomeClass(c.Hareket)
or use a value tuple[^]:
group c by (c.Hareket)
or an anonymous type:
group c by new { c.Hareket }
But since you're only grouping by a single value, you don't actually need to wrap it in another class at all:
group c by c.Hareket

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900