例如,我有一個類象下面這樣:Linq如何實現它?
public class SampleItem
{
public string QCD { get; set; }
public string CCD { get; set; }
public string ITYPE { get; set; }
}
然後,我有一個列表:
var lstTest = new List<SampleItem>() {
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C3" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "B"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "B"} ,
};
我想2組像
組1:
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C3" , ITYPE = "A"} ,
組2:
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "B"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "B"} ,
所以,我只需要
lstTest.GroupBy (p=>new {p.QCD ,p.ITYPE});
但現在,如果我的列表的順序是像下面
var lstTest = new List<SampleItem>() {
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "B"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "B"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C3" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "B"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "B"} ,
};
一個我怎樣才能得到3組像
第1組:
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "B"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "B"} ,
組2:
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "A"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C3" , ITYPE = "A"} ,
組3:
new SampleItem(){ QCD = "Q1" , CCD = "C1" , ITYPE = "B"} ,
new SampleItem(){ QCD = "Q1" , CCD = "C2" , ITYPE = "B"} ,
通過LINQ
?
非常感謝您的任何建議。
什麼你想要的,然後,它分裂時,值*更改*,而不是一個完整的組 - 沒有內置的LINQ操作;你將不得不手動編寫它。我會在一瞬間刺傷... – 2012-01-05 12:56:09
組1與組3相同? – V4Vendetta 2012-01-05 12:58:43
謝謝你的評論,是的,我已經通過循環實現它。但是,實際上這只是我的一部分代碼。我想找一個更清楚的方法來解決這個問題。 :) – shenhengbin 2012-01-05 13:00:19