简体   繁体   English

Linq to Entities和连接属性

[英]Linq to Entities and concatenated properties

Does anyone know if its possible to create a new property on an existing Entity Type which is based on 2 other properties concatenated together? 有没有人知道是否可以在现有实体类型上创建一个新属性,该实体类型基于连接在一起的其他2个属性?

Eg My Person Entity Type has these fields "ID", "Forename", "Surname", "DOB" 例如,我的人员实体类型包含这些字段“ID”,“Forename”,“Surname”,“DOB”

I want to create a new field called "Fullname" which is 我想创建一个名为“Fullname”的新字段

Forenames + " " + Surname

So i end up with "ID", "Forename", "Surname", "DOB", "Fullname". 所以我最终得到了“ID”,“Forename”,“Surname”,“DOB”,“Fullname”。

I know i can do this using Linq programmatically ie 我知道我可以通过编程方式使用Linq来做到这一点

var results = from p in db.People
select new { 
ID = p.ID, 
Forename = p.Forename, 
Surname = p.Surname, 
DOB = p.DOB,
Fullname = p.Forename+ " " + p.Surname
};

Then calling something like 然后打电话给像

var resultsAfterConcat = from q in results 
where q.Fullname.Contains(value)
select q;

However i'd really like to use Linq to Entities to do this work for me at the Conceptual Model level. 但是我真的很想使用Linq to Entities在概念模型级别为我做这项工作。

Not yet, but maybe soon. 还没有,但也许很快。 First, note that your suggested query will not work at all in LINQ to Entities, with or without the property, because, at present, it doesn't support Contains. 首先,请注意,建议的查询在LINQ to Entities中根本不起作用,有或没有属性,因为目前它不支持Contains。 The new version of the Entity Framework in .NET 4.0, however, is supposed to support custom methods in LINQ to Entities queries. 但是,.NET 4.0中新版本的Entity Framework应该支持LINQ to Entities查询中的自定义方法。 You can see a video about this from PDC . 您可以在PDC上看到有关此内容的视频 Essentially, you have to write the custom method twice; 基本上,你必须两次编写自定义方法; once in code, and once on your database (eg, in a calculated field). 一次在代码中,一次在您的数据库上(例如,在计算字段中)。 See the video for more information. 有关更多信息,请参阅视频。

For anyone happening in to read this so many years after the question has been answered: 对于在问题得到解答后这么多年来发生这种情况的人来说:

There is a more up to date and more DRY compliant answer here: Using a partial class property inside LINQ statement 这里有一个更新,更符合DRY的答案: 在LINQ语句中使用部分类属性

包含“作品”的原因是因为你正在调用String.Contains而不是IEnumerable.Contains,就像Craig想的那样。

Craig, 克雷格,

Sarted watching the video, then realised it's over an hour long, so will have to watch it when i have more time. 观看视频,然后意识到它超过一个小时,所以当我有更多时间时,必须观看它。 Just to let you know though.. Contains seems to be working ok for me, here's the SQL that's generated by Linq to Entities: 只是为了让你知道.. 包含似乎对我有用,这里是由Linq生成的SQL:

SELECT 
1 AS [C1], 
[Extent1].[PeopleID] AS [PeopleID], 
[Extent1].[Forenames] AS [Forenames], 
[Extent1].[Surname] AS [Surname]
FROM [dbo].[People] AS [Extent1]
WHERE (CHARINDEX(N'Dave', [Extent1].[Forenames] + N' ' + [Extent1].[Surname])) > 0

It seems to work a treat. 这似乎是一种享受。 Using CHARINDEX to workout if the Concatinated field contains the entered text which is the above case was " Dave ". 如果Concatinated字段包含输入的文本,则使用CHARINDEX进行训练,这是上面的情况是“ Dave ”。

Thanks Dave 谢谢戴夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM