尝试从两个列表创建列表

时间:2017-02-18 07:11:37

标签: c# linq

以下是我的课程:

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; }
    }

以下是两种方法:

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

testList的记录:

[0] : 100
      List of VariantRanks

[1]: 101
      List of VariantRanks

testListfromDb的记录:

[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>();

情景:

1

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)

2

    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)

第3

 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)

我已经完成了第三种情况,但我没有得到如何解决第一和第二种情况。

代码:

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];
        }

1 个答案:

答案 0 :(得分: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