如何在实体框架中使用SQL的LIKE对guid? [英] How to use SQL's LIKE on a guid in Entity Framework?

查看:425
本文介绍了如何在实体框架中使用SQL的LIKE对guid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL Server中, uniqueidentifier 列可以像LIKE比较的字符串一样使用:

  SELECT * FROM dbo.Table WHERE GuidColumn LIKE'4c38e01e%'

如何在Entity Framework中使用此功能?



我想要的东西

  context.Table.Where(x => x.StringColumn.Contains(some string))

但是对于Guid列而不是字符串列:

  context.Table.Where x => x.GuidColumn ????4c38e01e)


解决方案>

据我所知,没有办法在EntityFramework中将Guid转换为字符串。
我知道一些工作,但他们不优雅,虽然他们可能在你的情况下有用:


  1. 创建在SQL Server中将您的Guid列转换为字符串的视图。然后创建一个新实体并将其映射到视图。现在你有你的guid列在字符串represantation和可以应用like运算符。主要缺点是查询性能。

  2. 创建PERSISTENT计算的字符串类型的列。使用convert表达式将您的Guid转换为字符串。查询效果与普通字符串列相同,但将记录插入表格需要较长时间。

  3. 如果这不是应用于数据源的唯一过滤器那么您可以在数据已经在内存中时应用您的GUID LIKE操作。

希望它有帮助。


In SQL Server, uniqueidentifier columns can be used like strings with the LIKE comparison:

SELECT * FROM dbo.Table WHERE GuidColumn LIKE '4c38e01e%'

How can I use this feature in Entity Framework?

I want something like this

context.Table.Where(x => x.StringColumn.Contains("some string"))

but for a Guid column instead for string columns:

context.Table.Where(x => x.GuidColumn ???? "4c38e01e")

解决方案

As far as I know there is no way to convert Guid to string on the fly in EntityFramework. I'm aware of some workaraounds but they are not elegant to me though they may be helpful in your case:

  1. Create a view in SQL server where you convert your Guid column to string. Then create a new entity and map it to the view. Now you have your guid column in a string represantation and can apply 'like' operator. The main drawback is query performance.
  2. Create a PERSISTENT calculated column of string type. Use convert expression to convert your Guid to string. Query performance would be the same as for an ordinary string column, but it will take a bit longer to insert records into your table.
  3. If this is not the only filter applied to your data source then maybe you can apply your GUID LIKE operation when the data is already in memory.

Hope it helps.

这篇关于如何在实体框架中使用SQL的LIKE对guid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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