我想根据单词的第一个字母对项目进行分组,下面的查询工作正常。但我想修改它并包含此分组的所有字母,无论该项目是否存在。我该如何解决这个问题?
查询是:-
var qry = from row in item.Topics
group row by row.Title[0].ToString().ToLower() into groupedItems
orderby groupedItems.Key
select new Group<Topic>(groupedItems.Key, groupedItems);
听起来你想对范围a,b,c进行外部连接,...z?
有几种方法可以做到这一点。例如,您可以尝试:
var qry = from row in "LifeLikeThis"
group row by row.ToString().ToLower()
into groupedItems
orderby groupedItems.Key
select new {Key = groupedItems.Key, Items = groupedItems};
var alphaQry = from character in "abcdefghijklmnopqrstuvwxyz"
join grp in qry on character.ToString() equals grp.Key into joined
from joinGroup in joined.DefaultIfEmpty()
select new {Key = character.ToString(), Items = joinGroup == null ? null : joinGroup.Items};
其中的关键部分是joined.DefaultIfEmpty()
。有关外联接的详细信息,请参阅 101 个示例 - http://msdn.microsoft.com/en-us/vcsharp/ee908647#leftouterjoin