第一次 SQL 到 Linq 的转换

发布于 2024-12-27 05:58:45 字数 517 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫林﹌晚霞¤ 2025-01-03 05:58:45

您需要的第一件事是模拟数据库的数据上下文。下面是如何使用 linq to sql 执行此操作的示例。您还可以使用实体框架 (EF) 或任何其他提供程序来完成此操作。

创建表后,查询就会非常直接地进行转换:

var results = from insEntity in tablIns
              from progEntity in tablProg
              where insEntity.InsID equals progEntity.InsID
              select new { insEntity.InsID, progEntity.ProgName };

对于您提出的问题,这就是我认为有用的。将来最好写一些问题来解释你正在尝试做什么,你已经尝试过什么,然后你被困在哪里。这个问题应该足够具体,以便让你渡过下一个难关。


根据您的编辑:您的查询需要有小写 whereselect 并且需要以分号结束语句(假设它是 )。然后你的select语句需要选择一个新的对象。结果看起来像这样:

public IQueryable<tblInstitute> InsRepeater()
{
    return from Inst in _db.tblInstitutes 
           from Progs in _db.tblPrograms 
           where Inst.InstituteID equals Progs.InstituteID 
           select Inst; // for the current method header
           //select new { Inst.InstituteID, Progs.ProgramName }; // to use this one you'll have to create a new type with the properties you want to return
}

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:

var results = from insEntity in tablIns
              from progEntity in tablProg
              where insEntity.InsID equals progEntity.InsID
              select new { insEntity.InsID, progEntity.ProgName };

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 and select and it needs to end the statement with a semi-colon (assuming it is ). Then you select statement needs to select a new object. The results would look something like this:

public IQueryable<tblInstitute> InsRepeater()
{
    return from Inst in _db.tblInstitutes 
           from Progs in _db.tblPrograms 
           where Inst.InstituteID equals Progs.InstituteID 
           select Inst; // for the current method header
           //select new { Inst.InstituteID, Progs.ProgramName }; // to use this one you'll have to create a new type with the properties you want to return
}
七秒鱼° 2025-01-03 05:58:45

首先你需要
1.创建实体类。
2. 数据背景。
3. 定义关系
4.查询

参考

from I in tblIns
from P in tblProg 
Where I.InsID = P.InsID
Select I.InsID, P.ProgName 

First you need to
1. Create entity classes.
2. The data context.
3. Define relationships
4. Query

Reference

from I in tblIns
from P in tblProg 
Where I.InsID = P.InsID
Select I.InsID, P.ProgName 
行雁书 2025-01-03 05:58:45

假设您在数据库中正确设置了外键,您可以执行此操作,无需自己进行联接:

from x in db.tblProgs
select x.tblIns.id, x.Progname

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:

from x in db.tblProgs
select x.tblIns.id, x.Progname
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文