Linq to Entities和连接属性

时间:2022-06-04 20:50:44

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个属性?

E.g. 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 i.e.

我知道我可以通过编程方式使用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在概念模型级别为我做这项工作。

4 个解决方案

#1


4  

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. The new version of the Entity Framework in .NET 4.0, however, is supposed to support custom methods in LINQ to Entities queries. You can see a video about this from PDC. Essentially, you have to write the custom method twice; once in code, and once on your database (e.g., in a calculated field). See the video for more information.

还没有,但也许很快。首先,请注意,建议的查询在LINQ to Entities中根本不起作用,有或没有属性,因为目前它不支持Contains。但是,.NET 4.0中的新版本的实体框架应该支持LINQ to Entities查询中的自定义方法。您可以在PDC上看到有关此内容的视频。基本上,你必须编写两次自定义方法;一次在代码中,一次在您的数据库上(例如,在计算字段中)。有关更多信息,请参阅视频。

#2


3  

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语句中使用部分类属性

#3


2  

The reason Contains "works" for you is because you're calling String.Contains, and not IEnumerable.Contains, as Craig thought.

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

#4


0  

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

#1


4  

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. The new version of the Entity Framework in .NET 4.0, however, is supposed to support custom methods in LINQ to Entities queries. You can see a video about this from PDC. Essentially, you have to write the custom method twice; once in code, and once on your database (e.g., in a calculated field). See the video for more information.

还没有,但也许很快。首先,请注意,建议的查询在LINQ to Entities中根本不起作用,有或没有属性,因为目前它不支持Contains。但是,.NET 4.0中的新版本的实体框架应该支持LINQ to Entities查询中的自定义方法。您可以在PDC上看到有关此内容的视频。基本上,你必须编写两次自定义方法;一次在代码中,一次在您的数据库上(例如,在计算字段中)。有关更多信息,请参阅视频。

#2


3  

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语句中使用部分类属性

#3


2  

The reason Contains "works" for you is because you're calling String.Contains, and not IEnumerable.Contains, as Craig thought.

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

#4


0  

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