相关模型;
public class category_trans
{
[Key]
[Column(Order = 1)]
public int category_id { get; set; }
[Key]
[Column(Order = 2)]
public int language_id { get; set; }
public string name { get; set; }
}
目前Linq查询,工作:
IQueryable<category_trans> APC =
from ct in db.category_trans
from c in db.Categories
from l in db.ISO_Languages
where (
ct.category_id == c.ID
&& ct.language_id == l.ID
&& l.code.Substring(0,2) == culture
&& c.IsDeleted == false)
select ct;
我想对查询结果进行分组,以便得到不同的category_trans.name
(现在我得到了多个)。
尝试
IQueryable<category_trans> APC =
from ct in db.category_trans
from c in db.Categories
from l in db.ISO_Languages
where (
ct.category_id == c.ID
&& ct.language_id == l.ID
&& l.code.Substring(0,2) == culture
&& c.IsDeleted == false)
group ct by ct.name into g
select new
{
category_id = g.category_id,
name = g.name
};
给我g.category_id
和g.name
IGrouping <string,category_trans> does not contain a definition for 'category_id'...
为什么分组似乎失去对模型成员的引用以及如何修复它?
答案 0 :(得分:1)
由于组可以包含多个,因此您可以使用Key
属性作为名称:
IQueryable<category_trans> APC =
from ct in db.category_trans
from c in db.Categories
from l in db.ISO_Languages
where (
ct.category_id == c.ID
&& ct.language_id == l.ID
&& l.code.Substring(0,2) == culture
&& c.IsDeleted == false)
group ct by ct.name into g
select new
{
category_id = g.First().category_id,
name = g.Key
};
我使用First
来获取第一个category_id
,您可能想要使用不同的逻辑。