Linq Query To Fetch The Data Of Parent Table By Using List Of Child ForiegnKeyId [Resolved]

Posted by Kasani007 under LINQ on 5/4/2016 | Points: 10 | Views : 544 | Status : [Member] | Replies : 4
Linq Query To Fetch The Data Of Parent Table By Using List Of Child ForiegnKeyId


public class Parent
{
List<Child> childs{get;set;}
}

what i need is that

List<Parent> p=new List<Parent>();
p=p.Where(x=>x.childs.boyId==20); //this is not correct query.


pls help me..




Responses

Posted by: A2H on: 5/4/2016 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can use the Any method to get values from inside the list like given below
Please try with the below code
p1 = p.Where(x => x.childs.Any(k => k.boyId == 1)).ToList();

Complete Code
 List<Parent> p = new List<Parent>();
List<Child> lstchild = new List<Child>();
Parent objParent = new Parent();
objParent.ParentID = 1;
objParent.Name = "Test Name1";
Child chd = new Child();
chd.boyId = 1;
chd.name = "ChildName1";
lstchild.Add(chd);
objParent.childs = lstchild;
p.Add(objParent);

Parent objParent2 = new Parent();
List<Child> lstchild2 = new List<Child>();
objParent2.ParentID = 2;
objParent2.Name = "Test Name2";
Child chd2 = new Child();
chd2.boyId = 2;
chd2.name = "ChildName2";
lstchild2.Add(chd2);
objParent2.childs = lstchild2;
p.Add(objParent2);

List<Parent> p1 = new List<Parent>();

p1 = p.Where(x => x.childs.Any(k => k.boyId == 1)).ToList();


Thanks,
A2H
My Blog

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 5/4/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
Additional Reference : https://msdn.microsoft.com/en-us/library/bb534972(v=vs.100).aspx

Thanks,
A2H
My Blog

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kasani007 on: 5/5/2016 [Member] Starter | Points: 25

Up
0
Down
thanQ A2H
List<int> Ids=new List<int>();

p1 = p.Where(x => x.childs.Any(k => k.boyId == Ids)).ToList();


how can i write this..

thanks in advance.

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 5/5/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can use the below query
p1 = p.Where(x => x.childs.Any(k => ids.Contains(k.boyId))).ToList(); 


Complete Code

List<Parent> p = new List<Parent>();

List<Child> lstchild = new List<Child>();
Parent objParent = new Parent();
objParent.ParentID = 1;
objParent.Name = "Test Name1";
Child chd = new Child();
chd.boyId = 1;
chd.name = "ChildName1";
lstchild.Add(chd);
objParent.childs = lstchild;
p.Add(objParent);

Parent objParent2 = new Parent();
List<Child> lstchild2 = new List<Child>();
objParent2.ParentID = 2;
objParent2.Name = "Test Name2";
Child chd2 = new Child();
chd2.boyId = 2;
chd2.name = "ChildName2";
lstchild2.Add(chd2);
objParent2.childs = lstchild2;
p.Add(objParent2);

List<int> ids = new List<int>();
ids.Add(1);

List<Parent> p1 = new List<Parent>();

p1 = p.Where(x => x.childs.Any(k => ids.Contains(k.boyId))).ToList();


Thanks,
A2H
My Blog

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response