gpt4 book ai didi

c# - 无法创建类型为 'xxx' 的常量值。在此上下文中仅支持原始类型或枚举类型

转载 作者:太空狗 更新时间:2023-10-29 23:38:10 25 4
gpt4 key购买 nike

我有两个表,分别是 state 和 namelist。

状态

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

名单

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

我希望结果表为

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

我想要使用 linq 查询得到上述结果

我尝试了下面的代码,但返回错误为(无法创建类型为“xxx”的常量值。在此上下文中仅支持原始类型或枚举类型。)

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

有人知道如何解决这个问题吗?

最佳答案

你需要使用分组依据...

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

关于c# - 无法创建类型为 'xxx' 的常量值。在此上下文中仅支持原始类型或枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30773015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com