2017-05-02 17 views
0

List1は "isPermanent = true"を持ち、List2はfalseの値を持ち、さらに List1には別のキー " nextVacationDate "である。diff値を持つ同じタイプのリストを2つマージ

私は以下のようにこれらの組み合わせをしようとしていますが、私はまだ異なる値のために重複を取得する恐れがあります。私は両方のリストを1つのリストにマージする必要がありますList1最初に(永続的な従業員が最初に).. LINQを使用してこれを行うには良い方法はありますか?

public newList1 List1(string abcd) 
    { 
      var result = serviceMethod1(abcd); 
      var newList1 = new List<emp>(); 
      if (result == null) return null; 

      newList.AddRange(
       result.Select(x => new Model 
       { 
        firstName = x.FName, 
        secondName = x.SName, 
        address = x.Address, 
        employeeId = x.EmpId, 
        isPermanent = true, 
        nextVacationDate =x.VacDt, 
        salary = x.Bsalary 
       })); 

      return newList1; 


    } 

    public newList2 List2(string defg) 
    { 
     var result = serviceMethod2(defg); 
     var newList2 = new List<emp>(); 
     if (result == null) return null; 

     newList.AddRange(
      result.Select(x => new Model 
      { 
       firstName = x.FName, 
       secondName = x.SName, 
       address = x.Address, 
       employeeId = x.EmpId, 
       isPermanent = false, 
       salary = x.Bsalary 
      })); 

     return newList2; 


    } 
    private List<emp> EmployyeList(List<emp> newList1, List<emp> newList2) 
    { 
     var sortedEmpList1 = newList1.OrderBy(i => i.Fname); 
     var sortedEmpList2 = newList2.OrderBy(i => i.Fname); 

     List<MeterModel> combinedList = newList1.Union(newList2) as List<emp>; 

     return combinedList; 
    } 
+1

このフラグは_really平等のためimportant_されていない場合は、あなたの 'Model'クラスに' IEquatable を '実装してから使用することをお勧めします'newList1.Union(newList2)' –

+0

'List1'に余分なキー' isPermanent'がありますが、 'nextVacationDate'を意味しませんでしたか?また、両方とも 'new model'(' new emp'ですか?)なので、余分なキーにすることはできません。ちょうど 'List1'がキー値を持っています。あなたが望むのは、Linq拡張メソッド '.DistinctBy'です。その後、 '.OrderBy(i => new {!i.isPermanent、i.Fname})'を実行することができます。 – NetMage

答えて

2

あなたは重複を避けるために、第二リストをフィルタリングすることができます

newList1.Union(newList2.Where(emp2 => !newList1.Any(emp1 => emp1.employeeId == emp2.employeeId))) 
関連する問題