我无法使用LINQ查询解决此问题。
所以我们的表结构如下: Id || bug_category || bug_name || bug_details || bug_priority
我想首先按bug_category分组。对于每个bug_category,我想反过来按bug_priority分组。
基本上我想要这样的东西:
bug_category = AUDIO ::没有BUGS - > Critical = 3,Medium = 2,Low = 7 bug bug_category = VIDEO ::没有BUGS - > Critical = 5,Medium = 1,Low = 9 bug。
以下查询返回类别AND customer_priority的所有唯一组合:
(其中 RawDataList 只是具有上述结构的数据列表)
var ProceesedData = from d in RawDataList
group d by new { d.bug_category, d.bug_priority } into g
select new
{
g.Key.bug_category,
g.Key.bug_priority
};
以下查询返回类别,后跟该类别中的记录列表:
var ProceesedData = from d in RawDataList
group d by d.bug_category into g
select new { g.Key, records = g
};
但是我无法继续进行,因为ProcessedData(返回变量)是未知类型。有什么想法吗?
答案 0 :(得分:29)
我怀疑你想要(名字变得更加惯用):
var query = from bug in RawListData
group bug by new { bug.Category, bug.Priority } into grouped
select new {
Category = grouped.Key.Category,
Priority = grouped.Key.Priority,
Count = grouped.Count()
};
然后:
foreach (var result in query)
{
Console.WriteLine("{0} - {1} - {2}",
result.Category, result.Priority, result.Count);
}
或者(但稍后见):
var query = from bug in RawListData
group bug by new bug.Category into grouped
select new {
Category = grouped.Category,
Counts = from bug in grouped
group bug by grouped.Priority into g2
select new { Priority = g2.Key, Count = g2.Count() }
};
foreach (var result in query)
{
Console.WriteLine("{0}: ", result.Category);
foreach (var subresult in result.Counts)
{
Console.WriteLine(" {0}: {1}", subresult.Priority, subresult.Count);
}
}
编辑:如评论中所述,这将导致多个SQL查询。要获得类似的结果结构,但更有效率,您可以使用:
var dbQuery = from bug in RawListData
group bug by new { bug.Category, bug.Priority } into grouped
select new {
Category = grouped.Key.Category,
Priority = grouped.Key.Priority,
Count = grouped.Count()
};
var query = dbQuery.ToLookup(result => result.Category,
result => new { result.Priority, result.Count };
foreach (var result in query)
{
Console.WriteLine("{0}: ", result.Key);
foreach (var subresult in result)
{
Console.WriteLine(" {0}: {1}", subresult.Priority, subresult.Count);
}
}
答案 1 :(得分:27)
这是一种完成嵌套分组的简便方法。我已经在内存集合中对其进行了测试,无论您的特定数据库提供程序是否能够很好地处理它,或者它是否表现良好都是未知的。
假设您有两个属性,并希望按州和国家/地区进行分组:
var grouped = People
.GroupBy(l => new { l.State, l.Country})//group by two things
.GroupBy(l=> l.Key.Country)//this will become the outer grouping
foreach(var country in grouped)
{
foreach(var state in country)
{
foreach(var personInState in state)
{
string description = $"Name: {personInState.Name}, State: {state.StateCode}, Country: {country.CountryCode}";
...
}
}
}
答案 2 :(得分:6)
我认为你正在寻找类似的东西:
var processedData =
rawData.GroupBy(bugs => bugs.bug_category,
(category, elements) =>
new
{
Category = category,
Bugs = elements.GroupBy(bugs => bugs.bug_priority,
(priority, realbugs) =>
new
{
Priority = priority,
Count = realbugs.Count()
})
});
foreach (var data in processedData)
{
Console.WriteLine(data.Category);
foreach (var element in data.Bugs)
Console.WriteLine(" " + element.Priority + " = " + element.Count);
}
答案 3 :(得分:0)
你可以这样做
var retList = (from dbc in db.Companies
where dbc.IsVerified && dbc.SellsPCBs && !dbc.IsDeleted && !dbc.IsSpam && dbc.IsApproved
select new
{
name = dbc.CompanyName,
compID = dbc.CompanyID,
state = dbc.State,
city = dbc.City,
businessType = dbc.BusinessType
}).GroupBy(k => k.state).ToList();
List<dynamic> finalList = new List<dynamic>();
foreach (var item in retList)
{
finalList.Add(item.GroupBy(i => i.city));
}