3
我有一个具有一对多关系的数据库表[Table1]。
这个相关的表[Table2]有一个类型字段,它是一个字符串。linq-to-sql按连接表中的字符串列表组
Table 1 Table 2 Field | Type Field | Type ------|----- ---------|----- Id | int Id | int Table1Id | int Type | string
我想创建的类型的每个组合出现的频率, 的总结和我试图做的DB尽可能多的工作,因为它是把一切都写进内存太慢。
代码我有工作,但似乎重复我。
items
.Where(x => x.Table2s.Count > 1)
.Select(x => new
{
Type = x.Table2s.Select(y => y.Type)
})
.ToList() // into memory as string.join has no translation into sql
.Select(x => new
{
Type = string.Join(",", x.Type) // no sql translation
})
.GroupBy(x => x.Type) // can't work on IEnumerable[string]
.Select(x => new Result()
{
Type = x.Key,
Count = x.Count()
})
.OrderByDescending(x => x.Count)
.ToList();
有没有一种办法按该字符串列表,这样我可以做的DB分组,并减少在我的代码
为什么你'的string.join()'所有类型和组在那?你是否试图检测具有相同字段定义的类型的重复项? –
@JeroenvanLangen对不起,我试图显示table1中的记录已链接到表2类型的每种组合的次数: 类型a,b,c = 5 |类型a,c = 10 |类型d,f = 1 –
您可以将一个int字段添加到“表1”中,通过该字段可以制作标记枚举的类型元素。然后你可以按他们的总数分组。 –