Friday, February 20, 2015

LINQ to SP and LookUp operation

Below code will get the actual look up value from look up list. There is no way out to get look up value from the list which has look up column. We should get this value with look up list reference avail. in Entity Class.


We have Emp (Child list) and Dept (lookup list). Emp list has Dept lookup column pointing to Dept list's Title column.

EmpDataContext dc = new EmpDataContext("http://ramsqlbi:47407/sites/test/");

                EntityList<EmpEntity> empList = dc.GetList<EmpEntity>("Emp");

                var getName = (from emp in empList
                                         where emp.Dept.Title == "IT"
                                         select emp).First();

                string lstrFirstName = getName.Title;

Above code will seek Dept "IT" in Dept list and display First Name from Emp list. Take a look at code "emp.Dept.Title". Emp object has Dept reference which points to Dept list and match Title column in Dept. list. This is the way to query lookup value.

No comments:

Post a Comment