合并两个模型列表

时间:2016-03-01 16:08:20

标签: c# linq

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

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;

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

1 个答案:

答案 0 :(得分:2)

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

var results = list1.Concat(article);

如果您需要新列表(不包括重复项),也可以使用Union

var results = list1.Union(article);

此外,List还有AddRange方法。

list1.AddRange(article);