2017-07-05 8 views
1

這些都是我的課:組對象共享相同的密鑰和扮演他們在另一個對象

class Schedule 
{ 
    Int32 Id {get; set;} 
    String CustomerName {get; set;} 
    DateTime SchedStartDate {get; set;} 
    DateTime SchedEndDate {get; set;} 
    List<Body> Body {get; set;} 
} 

class Body 
{ 
    Int32 Id {get; set;} 
    String ProductBarcode {get; set;} 
    Int32 ProductItemId {get; set;} 
    DateTime DtEvaluation {get; set;} 
    List<History> History {get; set;} 
} 

class History 
{ 
    Int32 ActionId { get; set; } 
    String Description { get; set; } 
    String Picture { get; set; } 
    String Observation { get; set; } 
    Int32 EvalItemId { get; set; } 
    Int32 EvalCriteriaId { get; set; } 
} 

我有一個Schedule對象和字符串Body.ProductBarcode重複Schedule.Body名單上。此外,有時列表Body.History是空的,有時不是。

Body.ProductBarcode從應用程序的角度來看,重複是正確的,但是當我嘗試合併數據時,它給我帶來了一些麻煩,因爲我希望它將Schedule對象投射到另一個類中(我覺得更容易同)工作:

class EmailSchedule 
{ 
    String Id { get; set; } 
    Int32 ProductId { get; set; } 
    DateTime DtEvaluation { get; set; } 
    List<EmailHistory> History { get; set; } 
} 
class EmailHistory 
{ 
    Int32 ActionId { get; set; } 
    String Description { get; set; } 
    String Picture { get; set; } 
    String Observation { get; set; } 
    String EvalItemId { get; set; } 
    Int32 EvalCriteriaId { get; set; } 
} 

請記住,在這一點上,我感興趣的唯一的事情就是Body.ProductBarcode鍵,每一個它的Body.History名單。 Schedules的屬性已經安全無虞,其他Body的屬性無用。

將被轉換成Schedule對象

我的問題是一個JSON的This is an example,由於有反覆Body.ProductBarcode我需要組,其中Body.ProductBarcode比賽,這之後將所有Body.History名單的出現相同的EmailSchedule.History列表。

我試過使用LINQ,但由於我不熟悉它,因此無法使其工作。

+0

當你說「組」和「帶」,你是什麼意思是什麼呢?如果它們與「ProductItemId」或「DtEvaluation」不同,如何分組? – NetMage

+0

我編輯了這個問題。此時唯一重要的屬性是「Body.ProductBarcode」。其他屬性是無用的。 –

+1

因此,在您的新EmailSchedule對象中,您不介意ProductId和DtEvaluation是否爲零或零? – NetMage

回答

1

我仍然不是100%確定你在找什麼結構,但是你可以試試這個,讓我知道它的接近你想要的位置嗎?

根據您的反饋,我可以調整它。 (schd是你的時間表對象)

var scheduleList = schd.Body.GroupBy(b => b.ProductBarcode).Select(b => b.Select(bod => new EmailSchedule 
{ 
    Id = s.Id.ToString(), 
    DtEvaluation = bod.DtEvaluation, 
    ProductId = bod.ProductItemId, 
    History = bod.History 
})).SelectMany(b => b);  //flatten to single list 

p.s.對於你的課程,使用public會很好,因爲否則它不會從json反序列化,並且因爲EmailHistoryHistory類是相同的,所以我只使用History。如果您確實需要將其轉換爲EmailHistory,則需要將代碼添加到行History = bod.History

+0

它沒有工作... scheduleList的結果沒有分組。 –

1

我認爲你不在乎任何EmailSchedule屬性,因爲你說除History之外其他Body屬性是無用的。如果你需要所有這些,你可以像我在這裏一樣複製其他History屬性到EmailHistory

這裏是混合查詢/方法的語法:

var ans = from b in bodies 
      group b by b.ProductBarcode into bg 
      let bgf = bg.First() 
      select new EmailSchedule { 
       History = bg.SelectMany(b => b.History, (b, h) => new EmailHistory { ActionId = h.ActionId, Description = h.Description }).ToList() 
      }; 

這裏是查詢語法:

var ans = from b in bodies 
      group b by b.ProductBarcode into bg 
      let bgf = bg.First() 
      select new EmailSchedule { 
       History = (from b in bg from h in b.History select new EmailHistory { ActionId = h.ActionId, Description = h.Description }).ToList() 
      }; 
相關問題