C#.NET LinQ从列表中删除Dupes

时间:2016-04-19 16:37:33

标签: c# linq

我有一个公司对象列表,其中包含姓名,地址,电子邮件等。我需要一种方法按公司名称对列表进行排序(我可以轻松地对其进行排序)。但后来我需要比较电子邮件和城市。如果其中一个与同名公司匹配,我需要同时删除它们。

我可以很容易地做到。首先得到一个并删除匹配的那个,但我也需要将原件从列表中删除。合理? 底部2 LINQ语句删除重复但不删除原始

for (int i = 2; i <= 10749; i++) //Range.UsedRange isnt working for some reason so I hard coded the row numbers
{
    firm = new Firm();
    string begCell;
    begCell = "B" + i;
    excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
    firm.acctNo = fixNullValues(Convert.ToString(excelCell.Value2));

    begCell = "C" + i;
    excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
    firm.name = fixNullValues(Convert.ToString(excelCell.Value2));

    begCell = "D" + i;
    excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
    firm.addy1 = fixNullValues(Convert.ToString(excelCell.Value2));

    begCell = "E" + i;
    excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
    firm.city = fixNullValues(Convert.ToString(excelCell.Value2));

    begCell = "F" + i;
    excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
    firm.state = fixNullValues(Convert.ToString(excelCell.Value2));

    begCell = "G" + i;
    excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
    firm.zip = fixNullValues(Convert.ToString(excelCell.Value2));

    begCell = "H" + i;
    excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
    firm.phone = fixNullValues(Convert.ToString(excelCell.Value2));

    begCell = "I" + i;
    excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
    firm.email = fixNullValues(Convert.ToString(excelCell.Value2));

    begCell = "AB" + i;
    excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range(begCell, begCell);
    if (Convert.ToString(excelCell.Value2).Equals("Active", StringComparison.OrdinalIgnoreCase))
        firm.active = true;

    if (firm.active)
        firmList.Add(firm);

}

var sortedList = firmList.OrderBy(o => o.name).ToList();
var removeDupEmails = firmList.GroupBy(x => x.name).SelectMany(x =>
                             x.OrderByDescending(y => y.email)
                             .Take(1)
                         );
var removeDupZips = firmList.GroupBy(x => x.name).SelectMany(x =>
                             x.OrderByDescending(y => y.addy)
                             .Take(1)
                         );

2 个答案:

答案 0 :(得分:0)

我认为这会奏效。而不是仅删除符合条件的那些。我更新了代码。发现了一些错误。

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            List<Company> companies = new List<Company>();
            var groups = companies.AsEnumerable().GroupBy(x => x.name)
                .Select(x => x.Where(y => (x.Select(z => z.email).ToList().Where(s => s == y.email).Count() == 1) && (x.Select(z => z.addy).ToList().Where(s => s == y.addy).Count() == 1)).Select(a => a)).SelectMany(b => b).ToList();


        }

    }
    public class Company
    {
        public string name {get;set;}
        public string email {get;set;}
        public string addy {get;set;}
    }

答案 1 :(得分:0)

要消除重复项,您可以使用以下模式:

source.GroupBy(element => element.Property) // Split the list in groups by the property you want to check
.Where(propertyGroup => propertyGroup.Count() > 1) // Filter the duplicates
.SelectMany(propertyGroup => propertyGroup) // Flatten back the result set

如果我正确理解了具体问题,您可以通过以下单个查询获得所需的结果:

var resultList = firmList
    .GroupBy(firm => firm.name)
    .OrderBy(nameGroup => nameGroup.Key)
    .SelectMany(nameGroup => nameGroup
        .GroupBy(firm => firm.email)
        .Where(emailGroup => emailGroup.Count() > 1)
        .SelectMany(emailGroup => emailGroup)
        .GroupBy(firm => firm.addy)
        .Where(addyGroup => addyGroup.Count() > 1)
        .SelectMany(addyGroup => addyGroup)
    )
    .ToList();