2016-04-28 9 views
0

フラットリストをネストされたリストに変換しようとしています。LINQネストされたリストを作成する

これは私のフラットなリストである:

public class DefectLocationCount { 
    public string LocationName { get; set; } 
    public string DefectName { get; set; } 
    public int TotalCount { get; set; } 
} 

これは私のネストされたリストである:これは、フラットリスト

var results = (from def in rep.tblDefects 
       join defLoc in 
        (from defDet in rep.tblMovementDefects 
        join det in rep.tblMovementDetails on defDet.MovementDetailId equals det.MovementDetailId 
        join loc in rep.tblLocations on det.ToLocationId equals loc.LocationId 
        join hed in rep.tblMovementHeaders on det.MovementHeaderId equals hed.MovemenHeaderId 
        where hed.DateCreated >= fromDate && hed.DateCreated <= toDate 
        select new { loc.LocationName, defDet.DefectId, loc.LocationId } 
        ) on def.DefectId equals defLoc.DefectId 
        group def by new { def.DefectName, defLoc.LocationId, defLoc.LocationName } into joined 
        select new 
        { 
         LocationName = joined.Key.LocationName, 
         LocationId = joined.Key.LocationId, 
         DefectName = joined.Key.DefectName, 
         TotalCount = joined.Count() 
        }).OrderBy(x => x.LocationId); 
を移入するために私のLINQクエリは

public class DefectLocationOutput 
{ 
    public string DefectName{ get; set; } 
    public List<int> TotalCount{ get; set; } 
} 

です

編集

は、これは私がList<DefectLocationOutput>の形式で enter image description here を達成しようとしていますし、代わりのnull値は0を持っているものです。

これはどのようにすることができますか?

答えて

1

私は本当にあなたが待っているかの結果を理解していませんでしたが、多分これはあなたを助ける:

DefectLocationCount cl1 = new DefectLocationCount() 
      { 
       DefectName = "Name1", 
       LocationName = "Location1", 
       TotalCount = 2 
      }; 

DefectLocationCount cl2 = new DefectLocationCount() 
      { 
       DefectName = "Name1", 
       LocationName = "Location2", 
       TotalCount = 3 
      }; 

DefectLocationCount cl3 = new DefectLocationCount() 
      { 
       DefectName = "Name2", 
       LocationName = "Location3", 
       TotalCount = 6 
      }; 

var lstCl = new List<DefectLocationCount>(); 
lstCl.Add(cl1); 
lstCl.Add(cl2); 
lstCl.Add(cl3); 

var result = lstCl.GroupBy(x => x.DefectName).Select(x => new DefectLocationOutput() { DefectName = x.Key, TotalCount = lstCl.Where(y => y.DefectName == x.Key).Select(y => y.TotalCount).ToList() }); 
+0

こんにちはジュリアは、あなたが私の編集にソリューションを変更することができます。あなたのソリューションからの結果は、それらが散在し、欠陥数が0の場所が出力リストの一部ではないことを除いて、正しいです。これが私の現在の目的です。 – JustLearning

+0

私はここで答えを見つけました:http://stackoverflow.com/questions/34334911/default-values-for-empty-groups-in-linq-groupby-queryそれは私の編集が示すように私に結果を与えた – JustLearning

関連する問題