联合2个列表,对出现在两个源列表中的ID进行额外的内容修改[英] Union 2 lists with additional content change for ID that occurs in both source lists

本文是小编为大家收集整理的关于联合2个列表,对出现在两个源列表中的ID进行额外的内容修改的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何合并这两个集合?

问题:当一个ApplicationID在listOfPM和listOfPM2中时,Test2为null,应该是一个数字

var listOfPM = from d in gbc
                select new Tab24PresentationModel
                {
                    ApplicationID = d.ApplicationID,
                    ApplicationName = d.ApplicationName,
                    Test1 = d.count,
                    Test2 = null
                };


var listOfPM2 = from d in gbc2
                select new Tab24PresentationModel
                {
                    ApplicationID = d.ApplicationID,
                    ApplicationName = d.ApplicationName,
                    Test1 = null,
                    Test2 = d.count
                };


var result = listOfPM.Union(listOfPM2);  

已尝试从 PM 列表中删除 Test2 并从 listOfPM2 中删除 Test1 并得到:

<块引用>

"xxx.Tab24PresentationModel"类型出现在单个 LINQ to Entities 查询中的两个结构不兼容的初始化中.一个类型可以在同一个查询的两个地方初始化,但前提是在两个地方都设置了相同的属性,并且这些属性的设置顺序相同.

我可以想办法使用多个 foreach 来解决这个问题.想使用 Linq!

推荐答案

非常感谢您的回答和评论 - 这是我想出的.

// take each list in turn and either add or update
var result = new List<Tab24PresentationModel>();
foreach (var t in listOfPM)
{
    var a = new Tab24PresentationModel
        {
            ApplicationID = t.ApplicationID,
            ApplicationName = t.ApplicationName,
            Test1 = t.Test1
        };

    result.Add(a);
}

// list2
foreach (var t in listOfPM2)
{
    // is this already in result
    if (result.Any(x => x.ApplicationID == t.ApplicationID))
    {
        var existing = result.First(x => x.ApplicationID == t.ApplicationID);
        existing.Test2 = t.Test2;
    }
    else
    {
        var a = new Tab24PresentationModel
            {
                ApplicationID = t.ApplicationID,
                ApplicationName = t.ApplicationName,
                Test2 = t.Test2
            };
        result.Add(a);
    }
}

本文地址:https://www.itbaoku.cn/post/1557066.html

问题描述

How to merge these 2 collections?

Problem: When an ApplicationID is in listOfPM and listOfPM2, Test2 is null and should be a number

var listOfPM = from d in gbc
                select new Tab24PresentationModel
                {
                    ApplicationID = d.ApplicationID,
                    ApplicationName = d.ApplicationName,
                    Test1 = d.count,
                    Test2 = null
                };


var listOfPM2 = from d in gbc2
                select new Tab24PresentationModel
                {
                    ApplicationID = d.ApplicationID,
                    ApplicationName = d.ApplicationName,
                    Test1 = null,
                    Test2 = d.count
                };


var result = listOfPM.Union(listOfPM2);  

Have tried removing the Test2 from list of PM and Test1 from listOfPM2 from each and get:

The type 'xxx.Tab24PresentationModel' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.

I can think of ways around this using multiple foreach's.. would like to use Linq!

推荐答案

Many thanks for the answers and comments - here is what I came up with.

// take each list in turn and either add or update
var result = new List<Tab24PresentationModel>();
foreach (var t in listOfPM)
{
    var a = new Tab24PresentationModel
        {
            ApplicationID = t.ApplicationID,
            ApplicationName = t.ApplicationName,
            Test1 = t.Test1
        };

    result.Add(a);
}

// list2
foreach (var t in listOfPM2)
{
    // is this already in result
    if (result.Any(x => x.ApplicationID == t.ApplicationID))
    {
        var existing = result.First(x => x.ApplicationID == t.ApplicationID);
        existing.Test2 = t.Test2;
    }
    else
    {
        var a = new Tab24PresentationModel
            {
                ApplicationID = t.ApplicationID,
                ApplicationName = t.ApplicationName,
                Test2 = t.Test2
            };
        result.Add(a);
    }
}