linq 2 sql 如何转换为 TSQL

发布于 2024-08-04 09:48:46 字数 441 浏览 3 评论 0原文

当您使用构造函数将 linq2sql 对象转换为域对象时,Linq2sql 似乎不知道如何构造 TSQL。如:

from c in db.Companies
select new Company (c.ID, c.Name, c.Location).Where(x => x.Name =="Roy");

但是当使用可设置属性时就可以了。

from c in db.Companies
select new Company { ID = c.ID, Name = c.Name, Location = c.Location }.Where(x => x.Name =="Roy");

我不想允许设置这些属性。我怎样才能实现这个目标?任何人都可以提供关于如何将 linq 2 sql 转换为 TSQL 的思考吗?提前致谢!

It seems Linq2sql doesn't know how to construct the TSQL while you transform linq2sql object into domain object with constructors. Such as:

from c in db.Companies
select new Company (c.ID, c.Name, c.Location).Where(x => x.Name =="Roy");

But when using settable attributes, it will be OK.

from c in db.Companies
select new Company { ID = c.ID, Name = c.Name, Location = c.Location }.Where(x => x.Name =="Roy");

I don't want to allow those attributes to be settable. How can I achieve this? And can anybody provide food for thought on how linq 2 sql is translated into TSQL? Thanks in advance!

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

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

发布评论

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

评论(3

天赋异禀 2024-08-11 09:48:46

这可能与 L2S 解析表达式的方式有关 - 它可以解析对象初始化表达式,但不能解析构造函数表达式。基本上,L2S 的工作方式是以任何 LINQ 提供程序的方式解析 linq 表达式。 然后将结果转换成SQL。

您可以通过首先将其转换为 IEnumerable 来实现您想要的效果,然后您就可以自由使用 LINQ 对象。在您给出的示例中,这很简单,但让我们概括为具有更复杂的 where 子句的情况:

var companyData =
    from c in db.Companies
    where c.Name.StartsWith("Roy")
    select new { c.ID, c.Name, c.Location };

var companies =
    from c in companyData.AsEnumerable()
    select new Company(c.ID, c.Name, c.Location);

It's probably to do with the way L2S parses the expressions - it can parse the object initialiser expression, but not the constructor expression. Basically, the way L2S works is to parse the linq expressions the way any LINQ provider does and then convert the result into SQL.

You could achieve what you'd like by converting it into an IEnumerable first, as then you'll be free to use LINQ to Objects. In the example you gave this is trivial, but let's generalise to a case with a more complex where clause:

var companyData =
    from c in db.Companies
    where c.Name.StartsWith("Roy")
    select new { c.ID, c.Name, c.Location };

var companies =
    from c in companyData.AsEnumerable()
    select new Company(c.ID, c.Name, c.Location);
心不设防 2024-08-11 09:48:46

第一个查询不正确,因为 from 语句将返回公司实体的集合。要仅获取 1 个公司,您需要将第一个语句更改为:

Company c = (from c in db.Companies where c.ID = someId select c).First();

第二个语句隐式执行 where 语句。

我建议您在执行第二个查询时运行SQL Profiler,以查看实际用作 TSQL 语句的内容。

The first query is incorrect because the from statement will return a collection of company entities. To get only 1 company you would need to change the first statement to:

Company c = (from c in db.Companies where c.ID = someId select c).First();

The second statement does the where statement implicitly.

I suggest you run SQL Profiler while executing the second query to see what is actually being used as TSQL statement.

压抑⊿情绪 2024-08-11 09:48:46

它无法转换涉及构造函数的查询,因为它不知道所述构造函数应该做什么。它具有属性的字段映射,但您的构造函数绝对可以做任何事情 - 为什么它应该尝试事后猜测呢?

无论如何,尚不清楚将 L2S 实体类的属性设置为只读的目的是什么 - 通过删除对象并使用相同的主键但新的属性值重新创建一个新对象,可以轻松地规避这一点。

It cannot translate a query involving a constructor, because it doesn't know what said constructor is supposed to do. It has field mappings for properties, but your constructor could do absolutely anything - why should it try to second-guess that?

It's not clear what the purpose of making the properties read-only would be for L2S entity classes anyway - it is trivially circumvented by deleting the object and re-creating a new one with the same primary key but new property values.

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