Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select multiple columns without join in LINQ

Tags:

c#

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.

like image 747
Harry Avatar asked Nov 26 '25 09:11

Harry


1 Answers

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 });
like image 101
Gilad Green Avatar answered Nov 27 '25 23:11

Gilad Green



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!