简体   繁体   English

使用linq从一个具有不同ID的列表中显示重复项

[英]Show duplicates from one list with different ID's using linq

I have a table with 我有一张桌子

ID  Name  ExcelID

1   a     1

2   b     1

3   a     2

4   b     2

5   c     2

I need to show the duplicates between two of the Excel ID's, so the output should show me 'C' as that is the unique value between excelid 1 & 2. For this reason i used the queries below to compare the two sets of data. 我需要显示两个Excel ID之间的重复项,因此输出应显示“ C”,因为这是excelid 1和2之间的唯一值。因此,我使用下面的查询来比较两组数据。

var assets = db.FPTStaticDataRatedFinancialAssetBase.OfType<FPTStaticDataRatedFinancialAssetBase>()
            .Where(c => c.FORATExcelId == fptexcel)
            .GroupBy(x => x.Name)
            .Select(y => y.FirstOrDefault()).ToList();


var assetsold = db.FPTStaticDataRatedFinancialAssetBase.OfType<FPTStaticDataRatedFinancialAssetBase>()
            .Where(c => c.FORATExcelId == fptexcelprevious)
            .GroupBy(x => x.Name)
            .Select(y => y.FirstOrDefault()).ToList();

I have grouped the data into two lists, one being excel id 1 and the second query being 2. 我将数据分为两个列表,一个是excel id 1,第二个是2。

However if i try to use the except method to show the distinct values, it just shows me all values from both lists 但是,如果我尝试使用except方法显示不同的值,它只会显示两个列表中的所有值

var uniqueItems = db.FPTStaticDataRatedFinancialAssetBase.OfType<FPTStaticDataRatedFinancialAssetBase>()
   .GroupBy(p => p.Name)
   .Where(p => p.Count() == 1)
   .Select(p => p.FirstOrDefault());

should do the trick? 应该做的把戏吗?

Trying it on the list you gave, it returned neatly item (5, "c", 2) 在给定的列表上尝试它,它整齐地返回了项目(5,“ c”,2)

You can search for names where you only get one result : 您可以搜索只有一个结果的名称:

var uniqueResult = db.FPTStaticDataRatedFinancialAssetBase.OfType<FPTStaticDataRatedFinancialAssetBase>()
        .Where(c => c.FORATExcelId == fptexcel || c.FORATExcelId == fptexcelprevious)
        .GroupBy(x => x.Name)
        .Where(c => c.Count() == 1)
        .Select(y => y.FirstOrDefault()).ToList();

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

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