Add child list inside parent list on matching condition - c#

I am having two lists
class A{ int id; string name; string type; List<B> listB;}
class B{int id; int refid; string value;}
ListofA[{id=1,name="ABC",type="A",listB=null},id=1,name="ABC",type="A",listB=null}]
ListofB[{id=4,refid=1,value="ABC"},{id=5,refid=1,value="DEF"},{id=6,refid=2,value="XYZ"},]
I want to add matching objects of B to listB of class A. condition should be A.id == B.id. I tried below approach but I am getting blank list.
ListofA = ListofA.Select(a => new A { id = a.id, name =a.name,type = a.type,
listB = listOfB.Where(b => b.refid == a.id).ToList()}).ToList();
Is there anything missed from me.
Can any one please help me in this?
Thanks in advance

I have rewritten your code removing the whole syntax errors there. So you will get the desired output (no blank list):
class A {public int id; public string name; public string type;public List<B> listB; }
class B { public int id;public int refid; public string value; }
static void Main(string[] args)
{
List<A> ListofA = new List<A>{
new A() { id = 1,name = "ABC",type = "A",listB = null},
new A() { id = 2,name = "XYZ",type = "A",listB = null}
};
List<B> listOfB = new List<B>{
new B() { id =4,refid=1,value="ABC"},
new B() { id=5,refid=1,value="DEF"},
new B() { id=6,refid=2,value="XYZ"}} ;
var ListofA2 = ListofA.Select(a => new A
{
id = a.id,
name = a.name,
type = a.type,
listB = listOfB.Where(b => b.refid == a.id).ToList()
}
).ToList();
}

I made a silly mistake, because of that I was getting blank list. I made the changes and its working good now. Thanks for your help

Related

How to use Generic to simplify duplicated code in C#

I have three classes A, B, Common.
public class A
{
public int Id { get; set; }
public string Name { get; set; }
}
public class B
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Common
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
I want to put all A and B into a collection of Common
var result = new List<Common>();
foreach (var i in A_collection) {
result.Add(new Common() {
Id = i.Id,
Name = i.Name,
Type = "This is A"
});
}
foreach (var i in B_collection) {
result.Add(new Common() {
Id = i.Id,
Name = i.Name,
Type = "This is B"
});
}
Please notice that I cannot modify any code in class A, B and Common. But I can add new classes
Is there a way to use generics to simplify the code to just use one loop like this?
foreach (var i in T_collection) {
result.Add(new Common() {
Id = i.Id,
Name = i.Name,
Type = "This is T"
});
}
If your use case allows you to use interface in the places of the objects, and A and B are not sealed class, then you can wrap A and B in your own class with an interface. Something like this:
public interface ICommon
{
int Id { get; set; }
string Name { get; set; }
}
public class MyA : A, ICommon {}
public class MyB : B, ICommon {}
then instead of creating new instances of A or B, you can create instances of MyA and MyB, which will allow you to work with ICommon while still being interchangeable for A or B, or wherever you declare objects of A or B, just replace them with MyA or MyB.
Use LINQ?
var lA = new List<A>();
var lB = new List<B>();
var a = lA.Select(a => new Common(){ Id = a.Id, Name = a.Name, Type = "This is A"});
var b = lB.Select(b => new Common(){ Id = b.Id, Name = b.Name, Type = "This is B"});
var all = a.Append(b).ToList();
Not impossible in technical terms. First you write methods to create Common from object...
public static Common GenerateCommon(object o)
{
if (o is A)
{
return GenerateCommon(o as A);
}
if (o is B)
{
return GenerateCommon(o as B);
}
throw new Exception("Object is neither A nor B");
}
public static Common GenerateCommon(A a)
{
return new Common()
{
Id = a.Id,
Name = a.Name,
Type = "This is A"
};
}
public static Common GenerateCommon(B b)
{
return new Common()
{
Id = b.Id,
Name = b.Name,
Type = "This is B"
};
}
Then concatenate two lists as list of objects.
List<A> alist = new List<A>();
alist.Add(new A() { Id = 1, Name = "asd" });
alist.Add(new A() { Id = 2, Name = "2dd" });
alist.Add(new A() { Id = 3, Name = "3q" });
List<B> blist = new List<B>();
blist.Add(new B() { Id = 4, Name = "4asd" });
blist.Add(new B() { Id = 5, Name = "5dd" });
blist.Add(new B() { Id = 6, Name = "63q" });
List<object> olist = alist.Select(x => (object)x).ToList();
olist.AddRange(blist.Select(x => (object)x));
List<Common> clist = new List<Common>();
foreach(var o in olist)
{
clist.Add(GenerateCommon(o));
}
But I think modifying A/B/Common or following #T.S.'s answer is better.
I think I'd just write an extension method pair:
public static Common AsCommon(this A a)
{
return new Common()
{
Id = a.Id,
Name = a.Name,
Type = "This is A"
};
}
public static Common AsCommon(this B b)
{
return new Common()
{
Id = b.Id,
Name = b.Name,
Type = "This is B"
};
}
And then e.g.:
listOfCommon.AddRange(listOfA.Select(AsCommon));
Or e.g.:
listOfCommon = listOfA.Select(AsCommon).Concat(listOfB.Select(AsCommon)).ToList();
Probably also worth pointing out that using AutoMapper can save you the hassle of writing code to copy an A or B to a new Common, because automapper can be set up to understand A->Common and then copy the matching property names. With some extra config it can also copy mismatched properties

C# select values of a property from a list inside a list

I have following objects
class A
{
public List<B> listB { get; set; }
}
class B
{
public int id { get; set; }
}
and in my application I have a scenario like below..
public void main()
{
var lstA = new List<A>();
var lstA = new List<A>();
var a1 = new A();
a1.listB = new List<B>
{
new B() { id = 1 },
new B() { id = 2 }
};
lstA.Add(a1);
a1 = new A();
a1.listB = new List<B>
{
new B() { id = 3 },
new B() { id = 4 }
};
lstA.Add(a1);
}
And I need to select all id's of B objects from lstA
Here is what I've tried so far
var ids = lst.Select(x=>x.listB.Select(y=>y.id)).ToList();
But It gives me a compilation error.
How can I do this?
You have to use SelectMany which flattens the lists:
var ids = lst.SelectMany(x => x.listB.Select(y => y.id)).ToList();
You are almost there, use SelectMany
var ids = lst.SelectMany(x=>x.listB.Select(y=>y.id)).ToList();
Check your Working Code
Here's what i did and it works perfectly
All i did was make the classes public and when you initialise List<B>, you add new List<B> because even though intellisense doesn't show you any error, when you run the application, you get object not referenced error
class Program
{
static void Main(string[] args)
{
var lstA = new List<A>();
var a1 = new A()
{
listB = new List<B>()
{
new B
{
id = 3
},
new B
{
id = 5
}
}
};
var a2 = new A()
{
listB = new List<B>()
{
new B
{
id = 1
},
new B
{
id = 8
}
}
};
lstA.Add(a1);
lstA.Add(a2);
var ids = lstA.SelectMany(r => r.listB.Select(x => x.id));
foreach (var id in ids)
{
Console.WriteLine(id);
}
Console.ReadKey();
}
}
public class A
{
public List<B> listB { get; set; }
}
public class B
{
public int id { get; set; }
}
try this to ignore duplicate id
var ids = lstA.SelectMany(x => x.listB.Select(y => y.id)).Distinct().ToList();

LinQ nested lists and nested selects

Consider these two tables:
ClassID Name
1 C1
2 C2
ClassID List<CourseSession>
1 [Object that has value "A"], [Object that has value "B"]
2 [Object that has value "B"], [Object that has value "C"]
When I join these two tables in Linq, I get:
ID Name List
1 C1 [A, B]
2 C2 [A, B]
Wheras I need to expand them:
ID Name List
1 C1 A
1 C1 B
2 C2 A
2 C2 B
Linq code:
var classes = from row in t.AsEnumerable()
select new
{
ClassID = row.Field<Guid>("ClassID"),
ClassName = row.Field<string>("Name"),
};
var classCourses = from row in classes.AsEnumerable()
select new
{
ID = row.ID,
CourseSessionList = GetAllCoursesByID(row.ID).AsEnumerable()
};
//Attempt to join
var expandedClassCourse = from classRow in classes
join ccRow in classCourses
on classRow.ID equals ccRow.ID
into filteredExpandedClasses
select filteredExpandedClasses;
I'm not sure how to achieve this. Any ideas?
Something like (not sure what your model looks like):
context.CouseSessions.Where(cs => /* condition goes here */)
.Select(cs =>
new
{
Name = cs.Name,
Class = cs.Class.Name
});
or
context.Classes.Where(c => /* condition goes here */)
.SelectMany(c => c.Courses)
.Select(cs =>
new
{
Name = cs.Name,
Class = cs.Class.Name
});
I created two models based on assumption. I hope this helps.
class Info
{
public int Id { get; set; }
public string Name { get; set; }
public List<string> List { get; set; }
}
class MyClass
{
public int Id { get; set; }
public string Name { get; set; }
public string s { get; set; }
}
static void Main(string[] args)
{
var infos = new List<Info> { new Info { Id = 1, Name = "c1", List = new List<string> { "A", "B" } }, new Info { Id = 2, Name = "c2", List = new List<string> { "A", "B" } } };
var myClasses = new List<MyClass>();
foreach (var info in infos)
{
myClasses.AddRange(info.List.Select(a => new MyClass { Id = info.Id, Name = info.Name, s = a }));
}
}
(from c in classList
join s in sessionList on c.ClassID equals s.ClassID
select new
{
ID = c.ClassID,
Name = c.Name,
SessionList = s.SessionList
})
.SelectMany(e => e.SessionList.Select(s => new
{
ID = e.ClassID,
Name = e.Name,
Session = s
}))

.Net List ForEach Item

I have 2 objects:
public class ClassA
{
public int Id
public string name;
public ClassB myObjectB;
}
public class ClassB
{
public int Id
public string name
}
Having 2 Lists for <ClassA> <ClassB>
Some items from List1 match by Id with an item on List2... I want to set the objectB foreach item...
foreach(ClassA item in List1)
{
ClassB obj = (from b in List2 where b.Id == item.Id select b).SingleOrDefault()
if(obj != null)
{
item.myObjectB = obj;
////////break; <- ignore this
}
}
This solution works for me, but I'm just wondering if there is a better way to do this, instead of Foreach
Thanks everyone for your help!!!
I think that a foreach, in this case, is actually the appropriate approach, as you're mutating your list. You could potentially simplify your code a bit, however:
foreach(ClassA item in List1)
{
item.myObjectB = List2.FirstOrDefault(b => b.Id == item.Id);
}
This will set the item every time, though it will be set to null if there is no match. If you already have items in myObjectB and setting them to null is inappropriate, you could use:
foreach(ClassA item in List1)
{
item.myObjectB = List2.FirstOrDefault(b => b.Id == item.Id) ?? item.myObjectB;
}
Expanding on Reed's answer.. You can actually do this in a one-liner, because a list has a ForEach method.
List1.ForEach(item => item.myObjectB = List2.FirstOrDefault(b => b.Id == item.Id) ?? item.myObjectB);
List<ClassA> list1 = new List<ClassA>();
List<ClassB> list2 = new List<ClassB>();
list1.Add(new ClassA { Id = 2, name = "a2" });
list1.Add(new ClassA { Id = 3, name = "a3" });
list1.Add(new ClassA { Id = 4, name = "a4" });
list1.Add(new ClassA { Id = 5, name = "a5" });
list2.Add(new ClassB { Id = 1, name = "b1" });
list2.Add(new ClassB { Id = 2, name = "b2" });
list2.Add(new ClassB { Id = 4, name = "b4" });
list2.Add(new ClassB { Id = 5, name = "b5" });
// Goal is to set ClassA::myObjectB from List1 to
// matching instance (if any) of ClassB from List2
var query =
from a in list1
from b in list2
where a.Id == b.Id
select Tuple.Create(a, b);
foreach (var element in query)
element.Item1.myObjectB = element.Item2;
Update:
Or if you really don't want a for loop, I just realized you can use the fact that assignments return a value and at the same time make an entry in an obfuscated code contest :)
(from a in list1
from b in list2
where a.Id == b.Id
select a.myObjectB = b).ToList();
Update2:
I just thought of an alternate approach - depending on your scenario, a lazy mechanism might work for you?
public class ClassA
{
public int Id
public string name;
private ClassB myObjectB;
public ClassB MyObjectB {
get { return myObjectB ?? (myObjectB = list2.FirstOrDefault(x => this.Id == x.Id)); }
}
}
With classes defined as follows:
class ClassA {
public int Id { get; private set; }
public string name { get; private set; }
public ClassB myObjectB { get; set; }
public ClassA(int pId, string pName) {
Id = pId;
name = pName;
}
}
class ClassB {
public int Id { get; private set; }
public string name { get; private set; }
public ClassB(int pId, string pName) {
Id = pId;
name = pName;
}
}
You can do the following using the LINQ Join method:
var listA = new List<ClassA> {
new ClassA(1, "OneA"),
new ClassA(2, "TwoA"),
new ClassA(3, "ThreeA")
};
var listB = new List<ClassB> {
new ClassB(1, "OneB"),
new ClassB(2, "TwoB"),
new ClassB(4, "FourB")
};
listA
.Join(
listB,
itemA => itemA.Id,
itemB => itemB.Id,
(itemA, itemB) => new { ItemA = itemA, ItemB = itemB }
).ForEach(pair => pair.ItemA.myObjectB = pair.ItemB);
listA.ForEach(itemA => Console.WriteLine(
"{0} maps to {1}",
itemA == null
? "null"
: itemA.name,
(itemA == null || itemA.myObjectB == null)
? "null"
: itemA.myObjectB.name
));
Output is:
OneA maps to OneB
TwoA maps to TwoB
ThreeA maps to null

Using FindAll on a List<List<T>> type

Assuming
public class MyClass
{
public int ID {get; set; }
public string Name {get; set; }
}
and
List<MyClass> classList = //populate with MyClass instances of various IDs
I can do
List<MyClass> result = classList.FindAll(class => class.ID == 123);
and that will give me a list of just classes with ID = 123. Works great, looks elegant.
Now, if I had
List<List<MyClass>> listOfClassLists = //populate with Lists of MyClass instances
How do I get a filtered list where the lists themselves are filtered. I tried
List<List<MyClass>> result = listOfClassLists.FindAll
(list => list.FindAll(class => class.ID == 123).Count > 0);
it looks elegant, but doesn't work. It only includes Lists of classes where at least one class has an ID of 123, but it includes ALL MyClass instances in that list, not just the ones that match.
I ended up having to do
List<List<MyClass>> result = Results(listOfClassLists, 123);
private List<List<MyClass>> Results(List<List<MyClass>> myListOfLists, int id)
{
List<List<MyClass>> results = new List<List<MyClass>>();
foreach (List<MyClass> myClassList in myListOfLists)
{
List<MyClass> subList = myClassList.FindAll(myClass => myClass.ID == id);
if (subList.Count > 0)
results.Add(subList);
}
return results;
}
which gets the job done, but isn't that elegant. Just looking for better ways to do a FindAll on a List of Lists.
Ken
listOfClasses.SelectMany(x=>x).FindAll( /* yadda */)
Sorry about that, FindAll is a method of List<T>.
This
var result = from x in listOfClasses from y in x where SomeCondition(y) select y;
or
var result = listOfClasses.SelectMany(x=>x).Where(x=>SomeCondition(x));
To keep a list of lists, you could do something like this example:
MyClass a = new MyClass() { ID = 123, Name = "Apple" };
MyClass b = new MyClass() { ID = 456, Name = "Banana" };
MyClass c = new MyClass() { ID = 789, Name = "Cherry" };
MyClass d = new MyClass() { ID = 123, Name = "Alpha" };
MyClass e = new MyClass() { ID = 456, Name = "Bravo" };
List<List<MyClass>> lists = new List<List<MyClass>>()
{
new List<MyClass>() { a, b, c },
new List<MyClass>() { d, e },
new List<MyClass>() { b, c, e}
};
var query = lists
.Select(list => list.Where(item => item.ID == 123).ToList())
.Where(list => list.Count > 0).ToList();
query would be List<List<MyClass>> holding lists of MyClass objects that passed the test. At first glance, it looks out of order with the Where extension coming after the Select, but the transformation of the inner lists needs to occur first, and that's what's happening in the Select extension. Then it is filtered by the Where.
I would probably go with this
List<List<string>> stuff = new List<List<string>>();
List<List<string>> results = new List<List<string>>();
stuff.ForEach(list=> {var result = list.FindAll(i => i == "fun").ToList();
if (result.Count > 0) results.Add(result);
});
List<string> flatResult = new List<string>();
stuff.ForEach(List => flatResult.AddRange(List.FindAll(i => i == "fun")));
That way you can go with a jagged array or flatten it out.. But the Linq way works well too :-).
While producing a flat List<MyClass> will answer your need most of the time, the exact answer to your question is:
var result = (from list in ListOfClassLists
let listWithTheId=
(
(from myClass in list
where myClass.ID == id
select myClass)
.ToList()
)
where listWithTheId.Count > 0
select listWithTheId
).ToList();
This code snippet was taken from my Proof of Concept:
using System.Collections.Generic;
using System.Linq;
namespace ListOfListSelectionSpike
{
public class ListSpikeClass
{
public List<List<MyClass>> ListOfClassLists { get; set; }
private List<MyClass> list1, list2, list3;
public ListSpikeClass()
{
var myClassWithId123 = new MyClass("123");
var myClassWithIs345 = new MyClass("456");
list1 = new List<MyClass> { myClassWithId123, myClassWithIs345 };
list2 = new List<MyClass> { myClassWithId123, myClassWithIs345, myClassWithId123 };
list3 = new List<MyClass> { myClassWithIs345, myClassWithIs345 };
ListOfClassLists = new List<List<MyClass>> { list1, list2, list3 };
}
public List<List<MyClass>> GetListOfListsById(string id)
{
var result = (from list in ListOfClassLists
let listWithTheId =
((from myClass in list
where myClass.ID == id
select myClass)
.ToList())
where listWithTheId.Count > 0
select listWithTheId)
.ToList();
return result;
}
}
public class MyClass
{
public MyClass(string id)
{
ID = id;
Name = "My ID=" + id;
}
public string ID { get; set; }
public string Name { get; set; }
}
}

Categories

Resources