将 sql 转换为 linq 示例

发布于 2024-08-29 04:47:11 字数 312 浏览 10 评论 0原文

我有一个 sql 语句,但我无法让它在 linq 中工作。有人可以告诉我如何将以下 sql 语句编写为 linq 吗?

SELECT * FROM mobileApplication
LEFT JOIN videoMobile ON mobileApplication.id = videoMobile.mobileApplicationId
      AND videoMobile.videoId = 257

这是一个左连接,右表上有一个 where 语句。它可以在 sql server 2005 中运行,但我想用 linq 编写它。

I've got a sql statement, but I can't get it working in linq. Can someone show me how I can write the following sql statement as linq?

SELECT * FROM mobileApplication
LEFT JOIN videoMobile ON mobileApplication.id = videoMobile.mobileApplicationId
      AND videoMobile.videoId = 257

It's a left join with a where statement on the right table. It works in sql server 2005, but I'd like to write it in linq.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

可爱咩 2024-09-05 04:47:11

我没有验证语法,但试试这个......

var mobileApplications = from ma in mobileApplication
                         join vm in videoMobile on ma.id equals vm.mobileApplicationId into j1
                         from j2 in j1.DefaultIfEmpty()
                         where vm.videoId == 257
                         select ma;

I didn't verify the syntax, but try this...

var mobileApplications = from ma in mobileApplication
                         join vm in videoMobile on ma.id equals vm.mobileApplicationId into j1
                         from j2 in j1.DefaultIfEmpty()
                         where vm.videoId == 257
                         select ma;
念﹏祤嫣 2024-09-05 04:47:11

有一种产品可以为您做到这一点。我发现它非常有用。产品名称为Linqer。它不是免费的,但也不贵,并且提供 30 天的试用期。我发现很少有它无法转换的查询。它对我来说效果很好。

http://www.sqltolinq.com/

There is a product that will do this for you. I have found it very useful. The product name is Linqer. It is not free, but not expensive, and offers a 30 day trial. I have found very few queries it is not able to convert. It has worked well for me.

http://www.sqltolinq.com/
永不分离 2024-09-05 04:47:11

它类似于:

from ma in mobiledApplication.DefaultIfEmpty()
join vm in videoMobile on new { mobileApplicationId = ma.id, videoId = 257 } equals new { mobileApplicationId = vm.mobileApplicationId, videoId = vm.videoId } into videoMobileApplication
from vma in videoMobileApplication
select vma

如果键为空,则默认为键,并在连接条件上使用匿名对象将 257 合并到连接中。

我很确定对 257 使用 where 子句会达到相同的结果......

Its something like:

from ma in mobiledApplication.DefaultIfEmpty()
join vm in videoMobile on new { mobileApplicationId = ma.id, videoId = 257 } equals new { mobileApplicationId = vm.mobileApplicationId, videoId = vm.videoId } into videoMobileApplication
from vma in videoMobileApplication
select vma

The keys being the default if empty and using anonymous objects on the join criteria to incorporate 257 into the join.

I am pretty sure that using a where clause for the 257 will achieve the same result though...

握住我的手 2024-09-05 04:47:11

尝试这样的:

var query = 
    from m in mobileApplication
    join v in videoMobile 
        on m.id = v.mobileApplicationId and v.id = 257
        select m;

请参阅此处:

http: //msdn.microsoft.com/en-us/library/bb397676%28v=VS.100%29.aspx

http://msdn.microsoft.com/en-us/magazine/cc163400.aspx

Try some like this:

var query = 
    from m in mobileApplication
    join v in videoMobile 
        on m.id = v.mobileApplicationId and v.id = 257
        select m;

See here:

http://msdn.microsoft.com/en-us/library/bb397676%28v=VS.100%29.aspx

http://msdn.microsoft.com/en-us/magazine/cc163400.aspx

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