如何将此SQL查询转换为linq? [英] How do you transform this SQL query to linq?
本文介绍了如何将此SQL查询转换为linq?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在学习如何使用linq进行查询。
我坚持如何将以下sql查询转换为linq。
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..
推荐答案
你走在正确的轨道上。
You're on the right track.
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
};
但是。 ..你的查询需要使用加入 [ ^ ]。
欲了解更多详情,请参阅:
视觉表现SQL联接 [ ^ ]
C#中的101个LINQ样本 [< a href =https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811btarget =_ blanktitle =New Window> ^ ]
linq101-lambda [ ^ ]
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[^]
这篇关于如何将此SQL查询转换为linq?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文