【问题标题】:Linq to group by firstname lastname and select from a sub arrayLinq按名字姓氏分组并从子数组中选择
【发布时间】:2012-05-16 21:21:46
【问题描述】:

我正在使用实体框架并在数据库上执行查询,该数据库返回一个公司,而该公司又每个公司有很多联系人。

我有两种情况,我想对具有相同名字和姓氏的 MyContacts 进行分组。

虽然我可以遍历存储结果的新对象数组,但我使用的是实体框架,并且必须多次加载数据会很慢,所以如果可能的话,我希望将对象维护为 -在结果集中。

计划是我可以遍历结果数组,对 MyContacts 对象进行更改,并将对象更新到 EF 存储库中。

第一种情况是按名称对联系人列表进行分组,但我不确定如何在不创建新的类数据集的情况下进行分组。

第二种情况更复杂,我有一个 MyAccounts 列表(每个都有一个 MyContacts 列表),我想返回所有列表的 MyContacts,按名字和姓氏分组,并返回原始类如果可能的话。

非常感谢,克里斯。

我已经删除了数据访问,并在下面做了一个简单的例子:

class MyAccount
{
    public string accountName { get; set; }
    public List<MyContact> Contacts { get; set; }
}

class MyContact
{
    public string firstname { get; set; }
    public string lastname  { get; set; }
}

MyContact contactA = new MyContact() { firstname = "Chris", lastname = "b", ID = 100 };
MyContact contactB = new MyContact() { firstname = "Chris", lastname = "b", ID = 101 };
MyContact contactC = new MyContact() { firstname = "Peter", lastname = "Bread", ID = 102 };
MyContact contactD = new MyContact() { firstname = "James", lastname = "apple", ID = 103 };
MyContact contactE = new MyContact() { firstname = "Richard", lastname = "Brian", ID = 104 };
MyContact contactF = new MyContact() { firstname = "James", lastname = "apple", ID = 105 };

List<MyContact> contacts = new List<MyContact>();
contacts.AddRange(new MyContact[] { contactA, contactB, contactC, contactD, contactE, contactF } );
// how do i get a list of items, grouped by same first and lastname?

MyAccount companyA = new MyAccount() { accountName = "CompanyA", Contacts = new List<MyContact>() };
companyA.Contacts.AddRange(new MyContact[] { contactA, contactB, contactC });
MyAccount companyB = new MyAccount() { accountName = "CompanyB", Contacts = new List<MyContact>() };
companyB.Contacts.AddRange(new MyContact[] { contactA, contactB, contactC });
MyAccount companyC = new MyAccount() { accountName = "CompanyB", Contacts = new List<MyContact>() };
companyB.Contacts.AddRange(new MyContact[] { contactA, contactB, contactC, contactD, contactE });
List<MyAccount> companyList = new List<MyAccount>(new MyAccount[] { companyA, companyB, companyC });
// from the companyList, is there any way to get a list of MyContact types grouped by duplicate first and lastname?

【问题讨论】:

    标签: c# .net linq


    【解决方案1】:

    试试这个...

        var result = from c in contacts
                     group c by new { c.firstname, c.lastname } into g
                     select g.ToList();
    
        var result1 = from c in companyList.SelectMany(company => company.Contacts)
                      group c by new { c.firstname, c.lastname } into g
                      select g.ToList();
    

    现在你得到了列表的 IEnumerable。

    【讨论】:

    • 您好,感谢您的回复,我已将原始代码更改为包含一个 ID,此 ID 对于在结果集中访问很重要。如何做到这一点,有没有办法让结果返回一个 MyContact 类类型列表而不是匿名类型?我在原始问题中扩展了我的意图/要求,关于撰写个人通讯录的所有部分。谢谢。
    • 此外,联系人列表(包括重复项)应该存在于结果集中,在上面的示例代码中,重复项被删除。问候
    【解决方案2】:

    首先,如果您打算拥有大量数据,我强烈建议您使用HashSet 而不是List。主要区别在于HashSet 具有 O(1) 的碰撞测试速度(搜索重复属性),而如果您使用 List,它使用 O(n) 算法。据我了解,您的最终目的是合并不同地址簿的列表并获得独特的价值。如果你想合并两个 List 并且只获取唯一值,使用 linq 的 Union 函数,如果你想只获取两个列表中重复的值,使用 Instersect

    【讨论】:

      猜你喜欢
      • 2017-03-05
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      相关资源
      最近更新 更多