1

I have little problem with my LINQ query (nHibernate)

I need to have count of objects znak with equal property Symbol

My query:

            var tmp = (from znak in sesja.Query<Znak>()
                       group znak by znak.Symbol into r
                       select new { Name= r.Key.Name, SUM= r.Count() });

This query works, but I need to make object contains other properties of znak class.

In this case: select new { Name= r.Key.Name, SUM= r.Count() }); i can make new objects only from r.Key, (Symbol property). But I need other properties in my new object.

Is it possible ?

iskrzycki
  • 154
  • 2
  • 13

2 Answers2

4

I recommend using lambda Linq syntax:

var items = sesja.Query<Znak().AsEnumerable();

var newList = items.GroupBy(x=>x.Symbol).Select(
       x=> new { Name=x.Key.Name, Count = x.Count(), Items = x.ToList() });

read more about Linq syntax LINQ: Dot Notation vs Query Expression

I think that lambda syntax is more readable and looks much cleaner in code because it's more c# style not sql style.

Of course there will be no difference in IL code, always you can install tools like resharper, they can convert lambda syntax to sql-like syntax.

Community
  • 1
  • 1
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • I'm getting the following error: Expression of type CE.Klasy.Znak cannot be used for parameter of type System.Collections.Generic.IEnumerable`1[CE.Klasy.Znak] of method System.Collections.Generic.List`1[CE.Klasy.Znak] ToList[Znak](System.Collections.Generic.IEnumerable`1[CE.Klasy.Znak])”."} Error shows on both answers. I tried with Items = x.ToList() and Items = x.ToList() – iskrzycki Sep 19 '13 at 16:27
3

Try something like

var tmp = (from znak in sesja.Query<Znak>()
           group znak by znak.Symbol into r
           select new { Name= r.Key.Name, SUM= r.Count(), Items = r.ToList() });

Items property will contain actual objects in the group.

Andrei
  • 1,015
  • 1
  • 11
  • 19
  • Worth noting that naming a `Count()` property `SUM` is confusing. I know it's the OP's issue, but it's worth correcting. – Bobson Sep 18 '13 at 20:02
  • I'm getting the following error: Expression of type CE.Klasy.Znak cannot be used for parameter of type System.Collections.Generic.IEnumerable`1[CE.Klasy.Znak] of method System.Collections.Generic.List`1[CE.Klasy.Znak] ToList[Znak](System.Collections.Generic.IEnumerable`1[CE.Klasy.Znak])”."} Error shows on both answers. I tried with Items = x.ToList() and Items = x.ToList() Any Ideas ? – iskrzycki Sep 19 '13 at 16:32
  • @iskrzycki Try this: instead of `sesja.Query()`, do `sesja.Query().AsEnumerable()`. This might explain the behavior: [Expression of type 'MyEnum' cannot be used for parameter of type](http://stackoverflow.com/questions/14651624/expression-of-type-myenum-cannot-be-used-for-parameter-of-type) – Andrei Sep 19 '13 at 17:43
  • @AndreiTanas 'Better', but... **"Enumeration yielded no results"** if it's important, i'm using Nhivernate 3.1 with Fluent Nhibernate – iskrzycki Sep 19 '13 at 18:47