public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
public List<Employee> GetAllEmployees()
{
var AllRec = from a in DB.Table1
from B in DB.Table2
where a.Type == 12
B.Age == 26
select new Employee
{
ID = a.ID,
Name = a.Name
};
return AllRec.ToList();
}
How can i write this Linq Query using Linq LAMBDA. How can i perform this by using Lambda ?
but this is one case where the query syntax works much better than the lambda one.
You could also do:
from a in DB.Table1.Where(x=>x.Type == 12)
from b in DB.Table2.Where(x=>x.Age == 26)
select new Employee{ ID = a.ID, Name = a.Name};
That brings up the question, you're not using the things from Table2 at all...why do you have them in the query? You're not doing a join on any field at all, so all you will get is ID, Name pairs from table 1, but each entry will be in the result multiple
times (to be more precise, each entry will be in the result tableB.Where(x=>x.Age == 26).Count() number of times).
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
Marked as answer by joshuait on Apr 20, 2012 11:22 AM
That is just a sample and i will take some column values from Table2. Also I not only need lambda in where condition, all over the query wherever possible(This is my MAIN question). Please guide.
Ok...still...check the link....and also AVOID using the lambda syntax for joins....the lamdba syntax for joins is NOT nicer than the query systax, it is much more cumbersome. And they have the same performance.
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
Marked as answer by joshuait on Apr 20, 2012 11:22 AM
joshuait
Member
65 Points
114 Posts
linq query to Linq Lambda query
Apr 19, 2012 09:35 AM|LINK
public class Employee { public int ID { get; set; } public string Name { get; set; } } public List<Employee> GetAllEmployees() { var AllRec = from a in DB.Table1 from B in DB.Table2 where a.Type == 12 B.Age == 26 select new Employee { ID = a.ID, Name = a.Name }; return AllRec.ToList(); }How can i write this Linq Query using Linq LAMBDA. How can i perform this by using Lambda ?
Joshua
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: linq query to Linq Lambda query
Apr 19, 2012 09:44 AM|LINK
You CAN use a lambda like this:
http://stackoverflow.com/questions/346313/how-to-do-joins-in-linq-using-lambdas-and-the-expression-tree
but this is one case where the query syntax works much better than the lambda one.
You could also do:
from a in DB.Table1.Where(x=>x.Type == 12)
from b in DB.Table2.Where(x=>x.Age == 26)
select new Employee{ ID = a.ID, Name = a.Name};
That brings up the question, you're not using the things from Table2 at all...why do you have them in the query? You're not doing a join on any field at all, so all you will get is ID, Name pairs from table 1, but each entry will be in the result multiple times (to be more precise, each entry will be in the result tableB.Where(x=>x.Age == 26).Count() number of times).
blog: www.heartysoft.com
twitter: @ashic
joshuait
Member
65 Points
114 Posts
Re: linq query to Linq Lambda query
Apr 19, 2012 09:59 AM|LINK
That is just a sample and i will take some column values from Table2. Also I not only need lambda in where condition, all over the query wherever possible(This is my MAIN question). Please guide.
Joshua
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: linq query to Linq Lambda query
Apr 19, 2012 10:06 AM|LINK
Ok...still...check the link....and also AVOID using the lambda syntax for joins....the lamdba syntax for joins is NOT nicer than the query systax, it is much more cumbersome. And they have the same performance.
blog: www.heartysoft.com
twitter: @ashic
joshuait
Member
65 Points
114 Posts
Re: linq query to Linq Lambda query
Apr 20, 2012 11:21 AM|LINK
Thanks for your answer and valuable time.
Joshua