实体框架通配符和放大器; LINQ [英] Entity Framework wildcards & Linq

查看:156
本文介绍了实体框架通配符和放大器; LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能建立一个有效的Linq查询包含通配符



我见过各种各样的答案,这个问题该建议使用:

 。凡(实体=> entity.Name.Contains(FooBar的))
。凡(实体=>的实体。 Name.EndsWith(酒吧))
。凡(实体=> entity.Name.StartsWith(富))

或建造RawSql:

  VAR的CommandText = 
@SELECT场
从表
,其中场LIKE @search

VAR的查询=新的ObjectQuery<&档案GT;(CommandText中,背景);
query.Parameters.Add(新ObjectParameter(搜索,wildcardSearch));



第一个解决方案是行不通的,如果通配符不是在一个字符串的开头或结尾,例如, SEARCHTERM =富%吧



第二个解决方案,使用RawSql,不坐右我,感觉就像出一种廉价的方式。但它的工作。



第三个选项,我还没有尝试是创造一些东西,可以解析搜索词并构造一个有效的Linq查询,这是一件好事@ Slauma在以下链接2有一展身手。但这仍然如果通配符是不是在开头或搜索词的结尾是行不通的。



所以问题:是否有可能建立一个有效的Linq查询?其中包含通配符



编辑:
它值得一提的是,在使用Oracle数据访问组件(ODAC / ODP),但我这种情况的IM不。认为它使在这种情况下太大的差别。




链接:




1。像,在实体框架

2的准确和通配符搜索在搜索词条件



3的使用RawSql


解决方案

如果您使用的是EDMX文件作为你的实体模型的基础上,那么也许你可以尝试创建的概念模型功能了然后执行SQL中的LIKE。我不知道这是否会为甲骨文工作。然后,您应该能够做到像下面这样:

 。凡(实体=>赞(entity.Name富%吧))


Is it possible to construct a valid Linq query which contains wildcard characters?

I've seen various answers to this question which suggest using:

.Where(entity => entity.Name.Contains("FooBar"))
.Where(entity => entity.Name.EndsWith("Bar")) 
.Where(entity => entity.Name.StartsWith("Foo"))

OR constructing RawSql:

var commandText =
    @"SELECT field
    FROM     table
    WHERE    field LIKE @search";

var query = new ObjectQuery<Profile>(commandText, context);
query.Parameters.Add(new ObjectParameter("search", wildcardSearch));

The first solution wouldn't work if the wildcard was not at the beginning or end of a string, for example, searchTerm = "Foo%Bar".

The second solution, using RawSql, doesn't sit right with me and feels like a cheap way out. But it does work.

The third option I have yet to try out is to create something which can parse the search term and construct a valid Linq query, which is something @Slauma had a go at in link 2 below. But this still wouldn't work if the wildcard was not at the beginning or end of the search term.

So the question: Is it possible to construct a valid Linq query which contains wildcard characters?

EDIT: Its worth mentioning that in this instance im using Oracle Data Access Components (ODAC/ODP), but I don't think it makes much difference in this case.

links:

1."like" queries in Entity Framework

2.exact and wildcard searching conditional on search term

3.Using RawSql

解决方案

If you are using an EDMX file as the basis for your Entity Model then perhaps you could try creating a Conceptual Model Function that then performs a LIKE in SQL. I am not sure if this would work for Oracle. You should then be able to do something like the following:

.Where(entity => Like(entity.Name, "Foo%Bar"))

这篇关于实体框架通配符和放大器; LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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