Harry Harry - 1 year ago 79
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.

Answer Source

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 });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download