我有一個linq查詢返回的結果按第一個字母排序。有沒有辦法在該信件所指定的小組之前返回第一個字母?例如;單獨的linq結果通過第一個字母asp.net mvc 3
**A**
Acountants
Apothecary
**B**
Basketball
Biscuits
等等。我嘗試將這些結果分組,
var companyquery = (from c in db.Categories
group c by c.Name.Substring(0, 1)
into cgroup
select new
{
FirstLetter = cgroup.Key,
Names = cgroup
}).OrderBy(letter => letter.FirstLetter);
return View(companyquery);
,但得到的錯誤:
"The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery
1[<>f__AnonymousType3
1[System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NewAtAClick.Models.Category]'.'
所以,現在我使用的排序依據,而不是組_成_。這是我的問題;
var companyquery = (from c in db.Categories
orderby c.Name.Substring(0, 1)
select c);
這將以alphebetical順序返回它們。我試着說
letter1 = companyquery.ToString().SubString(0,1)
然後
return view(letter1 + companyquery.ToList());
但一無所獲。
在此先感謝!
您的組粘貼到代碼應工作正常,但瞭解它不會是直線名單,但部分,即組 –
我已經添加了我使用到組它的代碼。謝謝。 – Dan