LinQ到SQL以及所有数据库的选择 [英] LinQ to SQL and where is the choice for all database

查看:235
本文介绍了LinQ到SQL以及所有数据库的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的源代码:

  var res = from s in Splitting 
join c in Customer在s.CustomerId等于c.Id
其中c.Id == customrId
&&& c.CompanyId == companyId
select s;

在查看代码时,有一位成员说我的代码仅适用于一个SQL数据库,并建议我使用LinQ到实体加入,以便它可以适用于所有数据库。



我不明白,我认为即使使用其他数据库,我们将其添加到实体框架中。下面的代码也可以正常工作,对吧?



请指教。

解决方案>

有两种方式可以编写您的 LINQ



1. LINQ查询表达式(查询语法)(已完成)

  var res = from s in Splitting 
join c in Customer in s.CustomerId equals c.Id
其中c.Id == customrId
&安培;&安培; c.CompanyId == companyId
select s;

2.另一个是 LINQ查询扩展方法(点语法)

  var res = Splitting.Join (客户,
sp => sp.CustomerId,
cu => cu.Id,
(sp,cu)=> new {sp,cu})
.Where(s => s.cu.Id == customrId&& s.cu.CompanyId == companId)
.Select(s => s.sp);




对于联接,我更喜欢 query-syntax 。有一些细节,
查询语法隐藏,可以使它非常值得拥抱与它所带来的可读性的改进。然而,查询语法有点
更受限制



点法语法更简洁,但执行多个表连接是一场噩梦另一方面是有一些LINQ
操作只存在于点语法中:Single(),
First(),Count()等)。对于查询语法的这些限制,您可以使用
dot-syntax。


注意:在编译时,到标准查询。


I have the source code like this:

var res = from s in Splitting 
          join c in Customer on s.CustomerId equals c.Id
         where c.Id == customrId
            && c.CompanyId == companyId
        select s;

When reviewing code, one member said that my code applies for only one SQL db, and advised me to use LinQ to Entity with Join so that it will work for all databases.

I don't understand, I think, even with other db, we will add it to Entity Framework. And the code below will work correct too, right?

Please advise.

解决方案

There are two ways you can write your LINQ.

1.LINQ Query Expressions (query-syntax) (Which you have done)

var res = from s in Splitting 
          join c in Customer on s.CustomerId equals c.Id
         where c.Id == customrId
            && c.CompanyId == companyId
        select s;

2.Another is LINQ query extension methods (dot-syntax)

var res = Splitting.Join(Customer,
                 sp => sp.CustomerId,
                 cu => cu.Id,
                 (sp, cu) => new { sp, cu })
           .Where(s => s.cu.Id == customrId && s.cu.CompanyId == companId)
           .Select(s => s.sp);

For joins, I strongly prefer query-syntax.There are details that query-syntax hides that can make it well worth embracing with the improvement to readability it brings.However query-syntax is somewhat more limited than dot-syntax in other aspects.

dot-syntax is more concise but performing multiple table joins is a nightmare.The flip side is that there are a number of LINQ operations that only exist within the dot-syntax: Single(), First(), Count() etc.For these limitation of query-syntax you can use dot-syntax.

N.B : At compile time, all are converted to Standard Query.

这篇关于LinQ到SQL以及所有数据库的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆