Harry Harry - 8 months ago 44
C# Question

Select multiple columns without join in LINQ

Say you have a simple query such as for example:

SELECT p.name, p.age, c.course, c.lecture
FROM person p, college c;

Can something like this be achieved in LINQ? the reason I am trying to list a whatever is in those columns and iterate over them.


Yes. It is called cross join:

var result = (from p in person
              from c in college
              select new { p.Name, p.Age, c.Course, c.Lecture });

In method syntax:

var result = person.SelectMany(p => 
                       college.Select(c => new { p.Name, p.Age, c.Course, c.Lecture });