为什么LINQ不会返回特定的初始值?

时间:2013-02-03 18:34:28

标签: linq

我有以下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没有......?

编辑 _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

帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

行。首先,向花时间去看这个问题的人道歉。事实证明问题出在上面的层中,其中发生了一些分页。在GetGroupedContacts(=&gt; _getGroupedContacts())的结果上运行了一条LINQ,如下所示: -

GetGroupedContacts().Take(1000).Skip(1)

这自然总是跳过第一个结果。

完成扳手 dooburt签字!