问题描述
我一直在今早LinqToSQL令人费解的问题。我会试着用缩写的例子总结如下解释我的观点。
I have been puzzling over a problem this morning with LinqToSQL. I'll try and summarise with the abbreviated example below to explain my point.
我有DB两个表:
table Parent
{
ParentId
}
table Child
{
ChildId
ParentId [FK]
Name
Age
}
这些都在我的项目LinqToSQL等价类,但是,我已经写了,而不是使用LinqToSQL类,我想我的UI使用两个自定义模型类。
These have LinqToSQL equivalent classes in my project, however, I have written two custom model classes that I want my UI to use, instead of using the LinqToSQL classes.
从前端我的数据访问经过一个服务类,后者又调用库类,它通过LINQ查询数据。
My data access from the front end goes through a service class, which in turn calls a repository class, which queries the data via linq.
在仓库一级通过返回一个IQueryable的:
At the repository level I return an IQueryable by:
return from data in _data.Children
select new CustomModel.Child
{
ChildId = data.ChildId,
ParentId = date.ParentId
};
我的服务层,然后返回儿童的名单,该父前增加了母公司的额外查询的限制。
My service layer then adds an additional query restriction by parent before returning the list of children for that parent.
return _repository.GetAllChildren().Where(c => c.Parent.ParentId == parentId).ToList();
所以在这一点上,我得到的方法没有支持转换为SQL错误,当我运行的一切,我的自定义模型的c.Parent财产无法转换。 [该c.Parent属性的对象引用链接的父模型类。]
So at this point, I get the method has no supported translation to sql error when I run everything as the c.Parent property of my custom model cannot be converted. [The c.Parent property is an object reference to the linked parent model class.]
这一切是有道理的,所以我的问题是这样的:
That all makes sense so my question is this:
您可以提供的查询过程 有一些规则,将转换 predicate EX pression到正确的 一块SQL来在数据库中运行 因此不会触发错误?
我没有做太多工作,LINQ到现在这么原谅我缺乏经验,如果我没有解释这不够好。
I haven't done much work with linq up to now so forgive my lack of experience if I haven't explained this well enough.
此外,对于那些评论我选择的架构,我已经改变了它要解决这个问题,我只是玩弄的想法在这个阶段。我想知道是否有供将来参考答案。
Also, for those commenting on my choice of architecture, I have changed it to get around this problem and I am just playing around with ideas at this stage. I'd like to know if there is an answer for future reference.
非常感谢,如果有人可以提供帮助。
Many thanks if anyone can help.
推荐答案
首先,它引出了一个问题:为什么是库返回UI类型?如果回购返回的数据库类型,这不会是一个问题。考虑重构,使回购只涉及数据模型和用户界面做翻译末(任意组合物后)。
Firstly, it begs the question: why is the repository returning the UI types? If the repo returned the database types, this wouldn't be an issue. Consider refactoring so that the repo deals only with the data model, and the UI does the translation at the end (after any composition).
如果你的意思,并把它转化到数据库 - 那么基本上没有。可组合查询只能使用在LINQ到SQL模型中定义的类型,以及少数支持的标准功能。类似的事情最近想出了一个相关的问题,see这里。
If you mean "and have it translate down to the database" - then basically, no. Composable queries can only use types defined in the LINQ-to-SQL model, and a handful of supported standard functions. Something similar came up recently on a related question, see here.
有关某些情况下(不寻常的逻辑,但使用的LINQ到SQL模型中定义的类型化的),你可以在数据库中使用UDF和自己编写的逻辑(在TSQL) - 但只能使用LINQ于─ SQL(不EF)。
For some scenarios (unusual logic, but using the typed defined in the LINQ-to-SQL model), you can use UDFs at the database, and write the logic yourself (in TSQL) - but only with LINQ-to-SQL (not EF).
如果成交量不高,你可以使用LINQ到对象的最后一位。只需添加一个 .AsEnumerable()
之前,受影响的其中,
- 这会做逻辑这一点早在托管的.NET code(但predicate将不在数据库查询中使用):
If the volume isn't high, you can use LINQ-to-Objects for the last bit. Just add an .AsEnumerable()
before the affected Where
- this will do this bit of logic back in managed .NET code (but the predicate won't be used in the database query):
return _repository.GetAllChildren().AsEnumerable()
.Where(c => c.Parent.ParentId == parentId).ToList();
这篇关于LinqToSQL - 不支持转换为SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!