2017-02-18 50 views
0

下面创建列表是我的课:试图从两个列表

public class Test 
    { 
     public int TestId { get; set; } 
     public List<VariantsRank> VariantsRanks { get; set; } 
    } 

    public class VariantsRank 
    { 
     public string Name { get; set; } 
     public int Rank { get; set; } 
    } 

以下2种方法:

testList的
var testList = CreateDataFromList();//return List<Test> 
Var testListfromDb = GetDatafromDB();//return List<Test> 

记录:

testListfromDb的
[0] : 100 
     List of VariantRanks 

[1]: 101 
     List of VariantRanks 

记录:

[0] : 100 
     List of VariantRanks // Order of variants will be different here and that is important to me 

[1]: 101 
    List of VariantRanks // Order of variants will be different here and that is important to me 

下面是我的变量,我想最后的输出:

var final = new List<Test>(); 

方案:

testList = 100 and 101 
testListfromDb = 101 

Expected Output for 1st scenario : final = 100 and 101(2 records and this 101 record will be from testListfromDb) 

testList = 100 and 101 
    testListfromDb = 100 

    Expected Output for 2nd scenario : final = 100 and 101(2 records and this 100 record will be from testListfromDb) 

testList = 100 and 101 
    testListfromDb = 100 and 101 

    Expected Output for 3rd scenario : final = 100 and 101 (2 records and both will be from testListfromDb) 

我已经做了3次的情况,但我没有得到如何解决第一和第二方案。

代码:

var final = new List<Test>(); 
var testList = CreateDataFromList(list); //return List<Test> 
Var testListfromDb = GetDatafromDB();//return List<Test> 
    if (testListfromDb == null) 
      final = testList; 
    else 
     { 
      if (testList.Count == testListfromDb.Count) // 3rd scenario 
       final = testListfromDb; 

     } 

更新

public void Method() 
     { 
      var testList = new List<Test>(); 
      testList.Add(new Test { TestId = 100 }); 
      testList.Add(new Test { TestId = 101 }); 
      var testListfromDb = new List<Test>(); 
      var final = new List<Test>(); 

      //scenario 1 
      testListfromDb.Add(new Test { TestId = 101 }); 
      //expected output in final variable 
      final[0] = testList[0]; 
      final[1] = testListfromDb[0]; 

      //scenario 2 
      testListfromDb.Add(new Test { TestId = 100 }); 
      //expected output in final variable 
      final[0] = testListfromDb[0]; 
      final[1] = testList[1]; 

      //scenario 3 
      testListfromDb.Add(new Test { TestId = 100 }); 
      testListfromDb.Add(new Test { TestId = 101 }); 
      //expected output in final variable 
      final[0] = testListfromDb[0]; 
      final[1] = testListfromDb[1]; 
     } 
+0

如果您有两个列表,首先比较列表的大小,使最大列表成为最终列表。然后循环添加第二个列表,添加最终列表中尚未包含的项目? – JohnG

+0

我认为你是否赞成非答复? –

+0

Downvoter请给出downvoting的原因,因为它会帮助我和其他人在提问时再犯错误:) –

回答

3

我认为解决您的问题:

public List<Test> Process(List<Test> testList, List<Test> testListfromDb) 
{ 
    return 
     testListfromDb 
      .Concat(testList) 
      .GroupBy(x => x.TestId) 
      .SelectMany(x => x.Take(1)) 
      .ToList(); 
} 

如果更改Test这个定义:

public class Test 
{ 
    public int TestId { get; set; } 
    public string Source { get; set; } 
} 

那么这个代码可以测试的结果:

var scenario1 = Process(new List<Test>() 
{ 
    new Test { TestId = 100, Source = "Test" }, 
    new Test { TestId = 101, Source = "Test" } 
}, new List<Test>() 
{ 
    new Test { TestId = 101, Source = "DB" } 
}); 

scenario 1

var scenario2 = Process(new List<Test>() 
{ 
    new Test { TestId = 100, Source = "Test" }, 
    new Test { TestId = 101, Source = "Test" } 
}, new List<Test>() 
{ 
    new Test { TestId = 100, Source = "DB" } 
}); 

scenario 2

var scenario3 = Process(new List<Test>() 
{ 
    new Test { TestId = 100, Source = "Test" }, 
    new Test { TestId = 101, Source = "Test" } 
}, new List<Test>() 
{ 
    new Test { TestId = 100, Source = "DB" }, 
    new Test { TestId = 101, Source = "DB" } 
}); 

scenario 3

+0

确定upvoted你的答案,你的善意帮助我的努力,但1我不明白的是为什么你有使用Take(1) ?? –

+1

'.Take(1)'在数据库和测试列表中存在具有相同'TestId'的记录时删除重复的ID。 – Enigmativity