我有以下LINQ:-
private static IEnumerable<AlphabetisedContact> _getGroupedContacts(int clientid)
{
return _getLiteContacts(clientid).GroupBy(c => c.Name[0]).Select(
g => new AlphabetisedContact { Initial = g.Key, Contacts = g.ToList() }).OrderBy(g => g.Initial); }
哪一个返回除字母为a
的联系人之外的所有联系人?我是不是错过了什么?
编辑当前返回如下内容:-
[{
"$id": "1",
"Initial": "B",
"Contacts": [{
"$id": "2",
"ContactId": 12,
"Name": "Bryan Chiney",
"PrimaryContact": {
"$id": "3",
"Type": "Email",
"Value": "bazrith@hotmail.com"
},
"Avatar": "/images/avatars/default.jpg",
"UserId": null
}, {
"$id": "4",
"ContactId": 22,
"Name": "Bryan Billbags",
"PrimaryContact": {
"$id": "5",
"Type": "Email",
"Value": "bryan@atomic.com"
},
"Avatar": "/images/avatars/contacts/randomiser/1.jpg",
"UserId": 15
},
...
我希望第一个Initial
块包括我的A
联系人数量。。。例如,如果我添加一个名为Bryn McWinkawonk
的新联系人,他会出现,但Alice Alikemen
不会。。。?
edit_getLiteContacts()
调用使用传递的参数访问数据库:-
private static IEnumerable<LiteContact> _getLiteContacts(int clientid)
{
return _getContacts(clientid).Select(c => new LiteContact()
{ ContactId = c.ContactId, ContactType = Enum.GetName(typeof (ContactTypeObject.Type), c.Type), Name = c.Name, PrimaryContact = _marshallFirstContactDetail(c.ContactId),
Avatar = c.Avatar, UserId = c.UserId }).ToList();
}
_getContacts()
:-
private static IEnumerable<Contact> _getContacts(int clientid)
{
using (var ctx = new atomicEntities())
{
var contacts = from c in ctx.Contacts
where c.ClientId == clientid
select c;
return contacts.ToList();
}
}
_marshallFirstContactDetail()
:-
private static Model.Contact.ContactDetail _marshallFirstContactDetail(int contact)
{
return _marshallContactDetails(contact).FirstOrDefault();
}
_marshallContactDetails()
:-
private static IEnumerable<Model.Contact.ContactDetail> _marshallContactDetails(int contact)
{
using (var ctx = new atomicEntities())
{
var o = from d in ctx.ContactDetails
join cd in ctx.ContactDetailTypes on d.ContactDetailTypeId equals cd.ContactDetailTypeId
where d.ContactId == contact
select new Model.Contact.ContactDetail {Type = cd.Description, Value = d.Description};
return o.ToList();
}
}
示例数据
contact_id client_id contact_name contact_type
8 22 Cain Allan 2
9 23 Bazrith Banners 2
10 22 Spencer Grep 1
12 22 Bryan Chiney 1
13 22 Dave Carter 4
15 22 Steve Tite 8
16 22 Henry Laythorpe 8
17 22 Chris Barker 8
18 22 Simon Cox 2
19 22 Russell Jacobs 1
20 22 John Wyndham 2
21 22 Isabel March 5
22 22 Bryan Billbags 2
23 22 Stu Plum 2
24 22 Pete Sorensen 7
25 22 Tom Francis 1
26 22 Rich McCormick 1
27 22 Tim Cain 4
28 22 Alex Ray-Harvey 1
29 22 Ryan Bennett 1
30 22 Alice Griswald 3
31 22 Archibald Smyth 3
32 22 Benjamin Franklin 5
感谢您的帮助。
好。首先,向花时间研究这个问题的人致以深深的歉意。事实证明,问题出在上面的一层,那里发生了一些分页。在GetGroupedContacts
(=>_getGroupedContacts()
)的结果上运行了一段LINQ,如下所示:-
GetGroupedContacts().Take(1000).Skip(1)
这很自然,总是跳过第一个结果。
完成扳手dooburt签字!