Failed to create constant value of type "xxx". In this context, only primitive or enumeration types are supported.

I have two tables: state and nelelist.

condition

------------ id | state ------------ 1 |xxx 2 |yyy 

name list

 --------------- id|Name |stateid -------------- 1|aaa |1 2|abb |1 3|ccc |2 4|dds |2 

I want the result table to be

 ------------------ state |count ------------------- xxx | 2 yyy | 2 

I want to get the above result using linq query

I tried to execute the code below, but returns an as error (It is not possible to create a constant value of type "xxx". In this context, only primitive or enumeration types are supported.)

 var count= (from n in bc.db.state select new { states = n.state, count = (from c in bc.db.namelist where c.stateid == n.id select n).Count() }); 

Does anyone know how to solve this problem?

+5
source share
1 answer

You need to use a group on ...

 var list= (from n in bc.db.state join c in bc.db.namelist on n.id equals c.stateid group n by n.state into g select new { State = g.Key, Count = g.Count() }); 
+2
source

All Articles