I want to group the items based on first alphabet of the word and this below query works fine. But I want to modify it and include all letters to this grouping irrespective of whether the item exists or not. How ca开发者_运维技巧n I solve this?
The query is :-
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);
Sounds like you want to do an outer join on to the range a,b,c,... z?
There are a few ways of doing this. For example, you could try:
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};
The key part of this is the joined.DefaultIfEmpty()
. For more info on outer joins, see 101 samples - http://msdn.microsoft.com/en-us/vcsharp/ee908647#leftouterjoin
精彩评论