简体   繁体   English

LINQ查询返回一个由多个列组成的整个对象

[英]LINQ query returning the entire object grouped by multiple columns when one is distinct

I have a quandry I am trying to solve with LINQ but I haven't got a working solution yet. 我有多种解决方案,试图用LINQ解决,但还没有有效的解决方案。

I have a list of businesses coming back that contain large amounts of data. 我有一份回来的企业列表,其中包含大量数据。 I need to preserve all this data so I have access to it while grouping it and eliminating certain duplicates. 我需要保留所有这些数据,以便在对其进行分组并消除某些重复项时可以访问它。

So the main properties I am interested in grouping by would be Address1, Address2 and BusinessName. 因此,我对分组感兴趣的主要属性是Address1,Address2和BusinessName。

I want to group first by business name, then by address 1 and then by address 2, but ONLY when address 2 is distinct. 我想先按公司名称分组,然后按地址1分组,再按地址2分组,但仅当地址2不同时才分组。 The reason for this is I might have multiple ways of having the same address written and usually this relates to address2 being written differently which is fine, we will support that for right now and if its written as Suite 200 or Ste 200 it will be treated differently. 原因是我可能有多种方式来写入相同的地址,并且通常这与地址2的写入方式不同有关,这很好,我们会立即支持,如果将其编写为Suite 200或Ste 200,则将其处理不同。 This is needed to make sure we don't eliminate actual differences in the case of multiple offices being located in the same building(ie Ste 200 and Ste 100 are both the same business with different offices). 需要这样做,以确保在多个办公室位于同一建筑物中的情况下,我们不会消除实际差异(即Ste 200和Ste 100都是同一家企业,但办公室不同)。 However, I don't want to return the same addresses with the same list of address2's. 但是,我不想返回具有相同address2列表的相同地址。

var myNonDupOfficeList = officeList
    .GroupBy(o => new { o.Address1, o.Address2, o.BusinessName})
    .OrderBy(g => g.Key.BusinessName).ThenBy(g => g.Key.Address1).ThenBy(g => g.Key.Address2)
    .Select(o => o.FirstOrDefault()).ToList();

The code I already have written will do this fine, but the issue is I lose all the other data I need. 我已经编写的代码可以很好地解决此问题,但是问题是我丢失了我需要的所有其他数据。 If I include that data in the new { } object, then it adds differences that increase the number of groups that I don't want to search by. 如果我将这些数据包括在新的{}对象中,那么它会增加差异,从而增加我不想搜索的组的数量。 For instance I add City, State and Zipcode data, but for my intents, that is not relevant...the data isn't always correct with zipcodes so someone entering a wrong zipcode will produce another group for example, or someone putting St Louis or St. Louis or Saint Louis will all be different groups. 例如,我添加了城市,州和邮政编码数据,但是出于我的意图,这是不相关的...邮政编码并不总是正确的数据,因此输入错误邮政编码的人会产生另一个组,例如,或者放置圣路易斯的人或圣路易斯或圣路易斯将是不同的群体。

City, State and Address are not relevant for how I want to group by, but I NEED access to that data once its been grouped by BusinessName, Address1 and Address2. 城市,州和地址与我要分组的方式无关,但是一旦按BusinessName,Address1和Address2对数据进行分组,就需要访问该数据。 How can I achieve this using Linq? 如何使用Linq实现此目的?

I tried this in LinqPad against the Northwind database and I think it does what you are after - 我在LinqPad中针对Northwind数据库进行了尝试,我认为它可以满足您的要求-

Customers
    .GroupBy(i => new { i.Country, i.City})
    .OrderBy(i => i.Key.City)
    .ThenBy(i => i.Key.Country)
    .Select(i => new { Row = i.FirstOrDefault(), Cnt = i.Count()})
    .Dump();

I included a count so I could see how many items were in each group. 我包括了一个计数,所以我可以看到每个组中有多少个项目。

While its a little more work up front, the best idea would be to create a type containing just the fields you want and create a new instance of that type when you perform your initial query. 尽管尚需时日,但最好的想法是创建一个仅包含所需字段的类型,并在执行初始查询时创建该类型的新实例。

public class MyBusiness
{
    public string BusinessName { get; set; }
    public string BusinessAddress1 { get; set; }
    public string BusinessAddress2 { get; set; }
}

then 然后

 var myNonDupOfficeList = officeList
.GroupBy(o => new { o.Address1, o.Address2, o.BusinessName })
.OrderBy(g => g.Key.BusinessName).ThenBy(g => g.Key.Address1).ThenBy(g => g.Key.Address2)
.Select(o => new MyBusiness
{
    BusinessName = o.BusinessName,
    BusinessAddress1 = o.Address1,
    BusinessAddress2 = o.Address2
}).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM