第一次 SQL 到 Linq 的转换
我是 LINQ 新手,有人可以指导我如何在 Linq 中转换以下 SQL 查询吗?
Select tblIns.InsID, tblProg.ProgName
from tblIns, tblProg
Where tblIns.InsID = tblProg.InsID
我正在开发 MVC2 项目,我有 dataContext 和 Reposetories ,请在下面找到我需要此查询的代码:
public IQueryable<tblInstitute> InsRepeater()
{
return from Inst in _db.tblInstitutes
from Progs in _db.tblPrograms
Where Inst.InstituteID = Progs.InstituteID
Select Inst.InstituteID, Progs.ProgramName
}
I am new to LINQ, can somebody guide me that how can I convert the following SQL Query in Linq.
Select tblIns.InsID, tblProg.ProgName
from tblIns, tblProg
Where tblIns.InsID = tblProg.InsID
I am working on MVC2 Project, I have dataContext and Reposetories , please find below the code where I need this query:
public IQueryable<tblInstitute> InsRepeater()
{
return from Inst in _db.tblInstitutes
from Progs in _db.tblPrograms
Where Inst.InstituteID = Progs.InstituteID
Select Inst.InstituteID, Progs.ProgramName
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要的第一件事是模拟数据库的数据上下文。下面是如何使用 linq to sql 执行此操作的示例。您还可以使用实体框架 (EF) 或任何其他提供程序来完成此操作。
创建表后,查询就会非常直接地进行转换:
对于您提出的问题,这就是我认为有用的。将来最好写一些问题来解释你正在尝试做什么,你已经尝试过什么,然后你被困在哪里。这个问题应该足够具体,以便让你渡过下一个难关。
根据您的编辑:您的查询需要有小写
where
和select
并且需要以分号结束语句(假设它是 c#)。然后你的select语句需要选择一个新的对象。结果看起来像这样:The first thing you need is a data context which emulates your database. Here is an example of how to do this with linq to sql. You can also do it with entity framework (EF) or any other provider.
Once you have the tables created, the query then translates pretty straight forward:
With the question you have asked, this is as much as I think will be useful. In the future it's best to write questions explaining what you are trying to do, what you have tried, and then where you are stuck. The question should be specific enough to get you just over the next hump.
Per your edit: The query you have needs to have lowercase
where
andselect
and it needs to end the statement with a semi-colon (assuming it is c#). Then you select statement needs to select a new object. The results would look something like this:首先你需要
1.创建实体类。
2. 数据背景。
3. 定义关系
4.查询
参考
First you need to
1. Create entity classes.
2. The data context.
3. Define relationships
4. Query
Reference
假设您在数据库中正确设置了外键,您可以执行此操作,无需自己进行联接:
Assuming you have your foreign keys set up properly in the database you can just do this, there is no need to to the joins yourself: