Reputation: 3071
I have class object like this:
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; }
}
And I get that data in this way:
{
"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"}]}
]
}
What i want is, I want to get distinct P and append coresponding s to it ie. I want final object to contain data like this:
{
"obj":[
{"p":"1","usertype":"A","studyData":[{"study":"1"},{"study":"2"},{"study":"3"}]},
{"p":"2","usertype":"A","studyData":[{"study":"1"}]}
]
}
Any way by which I can achieve this in c# or linq?
Upvotes: 0
Views: 112
Reputation: 5733
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()
};
Modify class SD
to use 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();
}
}
Upvotes: 1
Reputation:
In this particular scenario you can use indexer to take particular List<SD>
corresponding to the value you are passing to the object
please go through the code that will explain it in detail;
Creation of Indexer
public List<SD> this[string index]
{
get
{
foreach (Obj item in ListObj)
{
if (item.p == index)
return item.sD;
}
return new List<SD>();
}
}
In the main function you can access the indexer and store Collect the details as the follows:
static void Main(string[] args)
{
ObjAssignStudy newInstance = new ObjAssignStudy();
List<SD> sampleData=newInstance["b"];// Gives the output
//collection as : EmptyList1,EmptyList2
}
Complete Code for your reference is here:
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>();
}
}
}
}
Test Cases: List sampleData=newInstance["a"];// Gives the output
EmptyList1
List<SD> sampleData=newInstance["a"];// Gives the output
EmptyList1 EmptyList2
List<SD> sampleData=newInstance["a"];// Gives the output
EmptyList1 EmptyList2 EmptyList3
Upvotes: 0