合并两个模型列表

本文关键字:两个 模型 列表 合并 | 更新日期: 2023-09-27 18:36:51

我想合并两个列表。列表 列表彼此包含不同的元素,我想将第一个列表合并到第二个列表:

var query = (
                from articleA in db.V_Article
                join articleC in db.V_CLIENT_PRIX on articleA.ARTICLE equals articleC.Article
                where articleC.CLIENT == Current_Client_Id
                select new
                {
                    ID = articleA.ID,
                    ARTICLE = articleA.ARTICLE,
                    PRIXVHT = articleC.PrixVHT,
                    REMISE = articleC.Remise,
                    STOCK = articleA.STOCK,
                    FOURNISSEUR = articleA.FRN,
                    FAMILLE = articleA.FAMILLE,
                    UNITE = articleA.Unite,
                    IMAGE = articleA.Image,
                    QUANTITE = 0,
                    CIN = Current_Client_Id,
                    REFERENCE = articleA.REFERENCE,
                    TVA = articleA.TVA,
                    PRIXVTTC = tarifclient == 0 ? articleA.PrixVTTC : tarifclient == 1 ? articleA.T1_PrixTTC : tarifclient == 2 ? articleA.T2_PrixTTC : articleA.T3_PrixTTC,
                });

        list1 = query.ToList() ;

这是第二个列表:

var article = (
                from articleA in db.V_Article
                where !tabEnc.Contains(articleA.ID)
                select new
                {
                    ID = articleA.ID,
                    ARTICLE = articleA.ARTICLE,
                    PRIXVHT = articleA.PrixVHT,
                    REMISE = 0,
                    STOCK = articleA.STOCK,
                    FOURNISSEUR = articleA.FRN,
                    FAMILLE = articleA.FAMILLE,
                    UNITE = articleA.Unite,
                    IMAGE = articleA.Image,
                    QUANTITE = 0,
                    CIN = Current_Client_Id,
                    REFERENCE = articleA.REFERENCE,
                    TVA = articleA.TVA,
                    PRIXVTTC = tarifclient == 0 ? articleA.PrixVTTC : tarifclient == 1 ? articleA.T1_PrixTTC : tarifclient == 2 ? articleA.T2_PrixTTC : articleA.T3_PrixTTC,
                }).ToList;

我想合并(连接)这两个列表 - 我该怎么做?

合并两个模型列表

尝试使用 Concat 方法。它返回包含重复项的新列表。

var results = list1.Concat(article);

如果您需要排除重复项的新列表,您也可以使用 Union。

var results = list1.Union(article);

此外,List具有AddRange方法。

list1.AddRange(article);