C# Linq并集,避免空数据和重复数据

C# Linq并集,避免空数据和重复数据,c#,linq,union,C#,Linq,Union,我正在尝试编写一个linq查询来合并2个集合,如下所述: public class VoteData { public string Name{get;set;} public string VoteFunction {get;set;} public string Area {get;set;} public string Sector {get;set;} public string Email {get;set;} public strin

我正在尝试编写一个linq查询来合并2个集合,如下所述:

public class VoteData
{
    public string Name{get;set;}
    public string VoteFunction  {get;set;}
    public string Area {get;set;}
    public string Sector {get;set;}
    public string Email {get;set;}
    public string VoterEmail {get; set;}
}

public class DirectoryData
{ 
   public string VoteFunction  {get;set;}
   public string Area {get;set;}
   public string Sector {get;set;}
   public string PrimaryEmail {get;set;}    
   public string VoterEmail {get; set;}

}
Linq查询:

 var result = (from r in voteList select new { r.Email, r.Area, r.Sector, r.VoteFunction }).Union
              (from d in directoryDataList select new { Email = d.PrimaryEmail, d.Area, d.Sector, d.VoteFunction });
现在假设此查询返回如下结果:

No Email Area Sector VoteFunction Voter Email ====================================================================== 1. abc@gmail USA IT Sales voter2@gmail 2. abc@gmail Null IT Analyst voter2@gmail 3. abc@gmail USA IT null voter2@gmail 4. abc@gmail Europe Tech null voter2@gmail 5. abc@gmail Europe Accounts Analyst voter2@gmail 6. abc@gmail Europe null Analyst voter2@gmail 7. abc@gmail USA IT null voter1@gmail ====================================================================== 无电子邮件区扇区VoteFunction投票者电子邮件 ====================================================================== 1.abc@gmail美国IT销售voter2@gmail 2.abc@gmail空IT分析师voter2@gmail 3.abc@gmail美国IT空voter2@gmail 4.abc@gmail欧洲技术中心voter2@gmail 5.abc@gmail欧洲账户分析师voter2@gmail 6.abc@gmail欧洲空分析员voter2@gmail 7.abc@gmail美国IT空voter1@gmail ====================================================================== 还有一个字段
VoterEmail
。因此,基本上,我想按
区域
扇区
VoteFunction
计算选票。 现在在上面的结果中,我想排除结果3和结果6。结果3,因为它有面积和部门,这也是目前的结果1为同一选民,而我仍然想保留结果7,因为它有不同的选民。与结果6类似,它具有面积和voteFunction,这也出现在结果5中


有什么解决方案吗?

所以您希望为(区域、扇区、投票者电子邮件)创建具有相同值的项目组。如果这些组中的任何一个包含多个项目,那么显然您有多个具有相同值的对象(区域、扇区、投票者电子邮件),并且您只想保留其中一个

您说过要排除no 3,因为它与no 1具有相同的值。类似地,我可以说我想排除no 1,因为它与no 3的值相同。你在乎是1还是3,还是我可以选择其中任何一个

var finalResult = unionResult
    .GroupBy(item => new     // make groups of items with same Area / Sector / VoterEmail
    {
        Area = item.Area,
        Sector = item.Sector,
        VoterEmail = item.VoterEmail,
    });
    // all items in every group have the same Area/Sector/VoterEmail
    .Select (group => group.First()); // from every group take the first one

结果战胜另一个结果的规则是什么。换句话说:为什么要保留结果1而排除结果3。为什么反之亦然?编辑了问题。还有一个字段叫做“noOfVotes”,如果区域、扇区和VoteFunctions的组合相同,则会产生相同的结果。所以对于同一个投票人,结果1和3将获得相同的区域和扇区结果。对于VoteFunction,我们不考虑结果3,因为它的VoteFunction为null。结果7号有相同的区域和扇区,但不同的投票者,所以在那里没有投票人会改变。我想一个
GroupBy
area,sector和VoteFunction会帮助你。不,这不符合我的目的。当我在某个区域分组并找到选票时,我得到的结果不准确。比如说,对于结果1和3,投票数是10,对于结果7,投票数是20。因此,在计算投票时abc@gmail,我得到计数到40(它是结果1、3和7的计票总数)。在结果1和3中,区域不是空的,所以我仍然会在GROUPBY中得到它。我需要的是voteCount 30,因为我想消除结果3,因为它的区域和扇区已经包含在结果1中。按区域、扇区和投票功能分组,并在两个类上发送投票者电子邮件,然后按两个结果分组,以形成联盟。:)