[英]Get unique objects From List of Objects
我有這樣的類對象:
public class SD
{
public string s { get; set; }
}
public class Obj
{
public string p { get; set; }
public string u { get; set; }
public List<SD> sD { get; set; }
}
public class ObjAssignStudy
{
public List<Obj> obj { get; set; }
}
我以這種方式獲取數據:
{
"obj":[
{"p":"1","usertype":"A","studyData":[{"study":"1"},{"study":"2"}]},
{"p":"2","usertype":"A","studyData":[{"study":"1"}]}
{"p":"1","usertype":"A","studyData":[{"study":"2"},{"study":"3"}]}
]
}
我想要的是,我想獲得不同的P並向其附加coresponding s。 我希望最終對象包含這樣的數據:
{
"obj":[
{"p":"1","usertype":"A","studyData":[{"study":"1"},{"study":"2"},{"study":"3"}]},
{"p":"2","usertype":"A","studyData":[{"study":"1"}]}
]
}
我可以通過任何方式在C#或linq中實現此目標?
var objs = new List<Obj>(){
new Obj
{
p = "1",
u = "A",
sD = new List<SD>() {new SD() { s = "1"}, new SD() { s = "2"}}
},
new Obj
{
p = "2",
u = "A",
sD = new List<SD>() {new SD() { s = "1"}}
},
new Obj
{
p = "1",
u = "A",
sD = new List<SD>() {new SD() { s = "2"}, new SD() { s = "3"}}
}
};
var distinct = from obj in objs
group obj by new { obj.p } into g
select new Obj {
p = g.Key.p,
u = g.First().u,
sD = g.SelectMany(i => i.sD).Distinct().ToList()
};
修改SD
類以使用Distinct
public class SD
{
public string s { get; set; }
public override bool Equals(object obj)
{
return string.Equals(s, (obj as SD).s);
}
public override int GetHashCode()
{
return s.GetHashCode();
}
}
在這種特殊情況下,您可以使用索引器獲取與要傳遞給object
的值相對應的特定List<SD>
,請仔細閱讀將詳細說明該代碼的代碼;
創建Indexer
public List<SD> this[string index]
{
get
{
foreach (Obj item in ListObj)
{
if (item.p == index)
return item.sD;
}
return new List<SD>();
}
}
在主要功能中,您可以訪問索引器並存儲收集詳細信息,如下所示:
static void Main(string[] args)
{
ObjAssignStudy newInstance = new ObjAssignStudy();
List<SD> sampleData=newInstance["b"];// Gives the output
//collection as : EmptyList1,EmptyList2
}
完整的代碼供您參考:
class Program
{
static void Main(string[] args)
{
ObjAssignStudy newInstance = new ObjAssignStudy();
List<SD> sampleData=newInstance["c"];
}
public class SD
{
public string s { get; set; }
}
public class Obj
{
public string p { get; set; }
public string u { get; set; }
public List<SD> sD { get; set; }
}
public class ObjAssignStudy
{
private List<Obj> _ListObj= new List<Obj>();
internal List<Obj> ListObj
{
get { return _ListObj; }
set { _ListObj = value; }
}
List<SD> tempList = new List<SD>();
public ObjAssignStudy()
{
tempList.Add(new SD() { s = "EmptyList1" });
ListObj.Add(new Obj() { p = "a", sD = tempList, u = "B" });
tempList.Add(new SD() { s = "EmptyList2" });
ListObj.Add(new Obj() { p = "b", sD =tempList, u = "C" });
tempList.Add(new SD() { s = "EmptyList3" });
ListObj.Add(new Obj() { p = "c", sD =tempList, u = "D" });
}
public List<SD> this[string index]
{
get
{
foreach (Obj item in ListObj)
{
if (item.p == index)
return item.sD;
}
return new List<SD>();
}
}
}
}
測試用例:列表sampleData = newInstance [“ a”]; //給出輸出
EmptyList1
List<SD> sampleData=newInstance["a"];// Gives the output
空清單1空清單2
List<SD> sampleData=newInstance["a"];// Gives the output
EmptyList1EmptyList2EmptyList3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.