7

私は2つのList<T>オブジェクトを持っています:

例えば:

リスト1:
ID、IDが入力され、値が空白で、1から10までのIDが含まれている値
。1、 ""
2、 ""
...
10、 ""

リスト2:
ID、値、およびその他の属性はすべて値で埋められていますが、このリストはIDの点でリスト1のサブセットです。(例:3アイテムのみ)
2,67
4,90
5,98

私が欲しいのは、マージされたリスト1ですが、値が更新されています。誰かがこれを実行する優れた拡張メソッドまたはこの操作を実行するためのエレガントなコードを持っていますか?最終的なリストは次のようになります。

ID、値
1、 ""
2,67//リストからの値23
、 ""
4,90
5,98
6、 ""
...
10、 ""

4

6 に答える 6

9

linq を使用: list1=list2.Union(list1);

于 2010-08-20T15:09:09.053 に答える
3

おそらく、リストではなく辞書を使用します。

    // sample data
    var original = new Dictionary<int, int?>();
    for (int i = 1; i <= 10; i++)
    {
        original.Add(i, null);
    }
    var updated = new Dictionary<int, int>();
    updated.Add(2, 67);
    updated.Add(4, 90);
    updated.Add(5, 98);
    updated.Add(11, 20); // add

    // merge
    foreach (var pair in updated)
    {
        original[pair.Key] = pair.Value;
    }

    // show results
    foreach (var pair in original.OrderBy(x => x.Key))
    {
        Console.WriteLine(pair.Key + ": " + pair.Value);
    }

オブジェクトのプロパティについて話している場合は、よりトリッキーになりますが、それでも実行可能です。

于 2009-08-19T10:02:05.250 に答える
3

これは O(m*n) ですが、任意のリストに対してジョブを実行する必要があります

        foreach (var record in List1)
        {
            var other = List2.FirstOrDefault(x => x.Key == record.Key);
            if(other != null) record.Value = other.Value;
        }

リストの順序が保証されている場合、より多くのコードを犠牲にして、リストを O(n) に下げることができます。アルゴリズムは次のようになります

Current items start as head of each list
While items remain in both lists
  If the current item of list1 has lower key than list2  advance to next in list1
  else if the current item of list2 has lower key than list1  advance to next in list2
  else copy value from current list2 item into list1 item and advance both lists.
于 2009-08-19T09:57:14.323 に答える
0
Dictionary<int, string> List1 = new Dictionary<int, string>();
List1.Add(1,"");
List1.Add(2,"");
List1.Add(3,"");
List1.Add(4,"");
List1.Add(5,"");
List1.Add(6,"");

Dictionary<int, string> List2 = new Dictionary<int, string>();
List2.Add(2, "two");
List2.Add(4, "four");
List2.Add(6, "six");

var Result = List1.Select(x => new KeyValuePair<int, string>(x.Key, List2.ContainsKey(x.Key) ? List2[x.Key] : x.Value)).ToList();
于 2016-11-07T07:53:48.620 に答える
0

両方のリストが ID でソートされている場合は、従来のマージ アルゴリズムのバリエーションを使用できます。

int pos = 0;
foreach (var e in list2) {
  pos = list1.FindIndex(pos, x => x.Id==e.Id);
  list1[pos].Value = e.Value;
}

これはまた、ID に関して のlist2厳密なサブセットである必要があることに注意してくださいlist1(つまり、list1実際にはのすべてのID が含まれますlist2) 。

もちろん、これを拡張メソッドでラップすることもできます

public static void UpdateWith<T>(this List<T> list1, List<T> list2) 
where T:SomeIdValueSupertype {
  int pos = 0;
  foreach (var e in list2) {
    pos = list1.FindIndex(pos, x => x.Id==e.Id);
    list1[pos].Value = e.Value;
  }
}
于 2009-08-19T10:19:28.473 に答える
0
 private void btnSearch_Click(object sender, EventArgs e)
{
String searchBy = cmbSearchBy.Text.ToString();
String searchFor = txtSearchFor.Text.Trim();

var List3 = (from row in JobTitleDB.jobList
                         where (row.JID.ToString()+row.JobTitleName.ToString().ToLower()).Contains(searchFor.ToLower())
                         select row).ToList();
if (searchBy == "All")
            {
                dgJobTitles.DataSource = null;
                //dgJobTitles.DataSource = List1;
                //dgJobTitles.DataSource = List2;
                //dgJobTitles.DataSource = List1.Concat(List2);
                //dgJobTitles.DataSource = List1.Union(List2);
                dgJobTitles.DataSource = List3;
                //dgJobTitles.DataSource=List1.AddRange(List2);
            }
}
于 2011-02-19T08:22:26.810 に答える