Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently learning how to query using linq.
I'm stuck on how to translate the following sql query to linq.

select (select top 1 fname + lastname from EmployeeList emp where stud.id = emp.childId) as [Parent Name],
   stud.name,
   stud.address
from Students stud


What I have tried:

var query = from stud in Students
              select new
              {
                Name = stud.name,
                Address= stud.address
              };


I dont know how to proceed. Please guide me. Thank you very much..
Posted
Updated 4-May-17 9:02am
v2

1 solution

You're on the right track.

C#
var query = from stud in Students
              select new
              {
                ParentName = (from emp in EmplyeeList where emp.childId = stud.id select new { pname = string.Concat(emp.fname, " ", emp.lastname) }).First()
                Name = stud.name,
                Address= stud.address
              };


But... your query needs optimization by using joins[^].


For further details, please see:
Visual Representation of SQL Joins[^]
101 LINQ Samples in C#[^]
linq101-lambda[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900