0

異なるデータを含むが類似した列を持つ2つのリストがあります。基本的に、これらのリストを結合したいのですが、同様の列を1にマージします。

var listCombo= List1.Where(a=>DataIds.Contains(a.dataId)).DefaultIfEmpty()
    .Join(List2,
    list1 => list1.key,
    list2 => list2.key,
    (L1,L2) => new
    {
        L2.key,
        L2.dataId,
        L2.dataValue,
        L2.date,
        L1.secId,
        L1.dataId,
        L1.dataValue
    });

dataId列とdataValue列を組み合わせたいのですが。それ、どうやったら出来るの?

したがって、一意の名前を付けるのではなく、listCombo.dataIdまたはlistCombo.dataValueとだけ言うことができます。

4

2 に答える 2

0

私があなたの質問を理解しているなら、あなたは2つのフィールドを1つに結合しようとしています。あなたが持っていると仮定してClass

public class List1 {
public int dataId {get;set;}
public string dataValue {get;set;}
public string combinedValue {get;set;}
}

いいえ、あなたはのように使うことができません、

var listCombo= List1.Where(a=>DataIds.Contains(a.dataId)).DefaultIfEmpty()
    .Join(List2,
    list1 => list1.key,
    list2 => list2.key,
    (L1,L2) => new List1
    {
       dataId = L2.dataId,
       dataValue = L2.dataValue
       combinedValue =  L2.dataId + " - " L2.dataValue
    });
于 2013-02-15T22:28:00.020 に答える
0

list1、list2、および両方の組み合わせである3番目のリストにPOCOメソッドを使用します。異なる型を組み合わせる場合は適切なキャストを行うか、複雑な型が必要な場合はそれらを独自のプロパティとして定義できるかどうかを確認します。

 static void Main(string[] args)
        {
            List<A> lisA = new List<A> {new A {AId = 1, AName = "Brett"}, new A {AId = 2, AName = "John"}};
            List<B> lisB = new List<B> {new B { BId = 1, BName = "Doe" }, new B { BId = 2, BName = "Howard" } };

            List<C> lisC = lisA.Join(lisB,
                                     list1 => list1.AId,
                                     list2 => list2.BId,
                                     (L1, L2) => new C
                                         {
                                             CId = L1.AId,
                                             CName = L1.AName + " " + L2.BName
                                         }).ToList();

            lisC.ForEach(
                n => Console.WriteLine(n.CName + "\n")
                );

        }

        public class A
        {
            public int  AId { get; set; }
            public string AName { get; set; }
        }

        public class B
        {
            public int BId { get; set; }
            public string BName { get; set; }
        }

        public class C
        {
            public int CId { get; set; }
            public string CName { get; set; }
        }
于 2013-02-15T21:48:18.733 に答える