MySQL Linq使用.Contains(variable) [英] MySQL Linq using .Contains(variable)

查看:187
本文介绍了MySQL Linq使用.Contains(variable)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置信息:


  • VS2013 / C#

  • EF6

  • MySQL数据库

  • .Net连接器6.9.5

  • VS2013 / C#
  • EF6
  • MySQL database
  • .Net Connector 6.9.5

我正在尝试创建一种使用部分名称作为搜索条件返回帐户记录集合的方法。如果我使用IQueryable .Contains()扩展方法硬编码字符串值,则返回数据。但是,当我尝试使用一个变量时,不返回数据。

I'm trying to create a method that returns a collection of Account records using a partial name as the search criteria. If I hard code a string value using the IQueryable .Contains() extension method, it returns data. However, when I attempt to use a variable no data is returned.

Public class Test() {

MyEntities db = new MyEntities();

//Works....but the search criteria is hard coded.
  public IQueryable<Account> WorksButValueHardCoded() {

    return (from a in db.Accounts
           where a.accountname.Contains("Test")
           select a);
  }

//Does not return anything
  public IQueryable<Account> DoesNotReturnAnyData() {

    //Obviously I would use a parameter, but even this test fails
    string searchText = "Test";  

    return (from a in db.Accounts
           where a.accountname.Contains(searchText)
           select a);
  }
}

我可以看到在LINQ生成的SQL中使用了LIKE操作符,但是我不明白变量是如何注入的:

I can see in the LINQ generated SQL used the LIKE operator, but I don't understand how the variable is injected as it reads:

SELECT
`Extent1`.`accountid`, 
`Extent1`.`accountname`
FROM `account` AS `Extent1`
WHERE `Extent1`.`accountname` LIKE '%p__linq__0%'

所以...为什么它可以用硬编码的值而不是字符串变量?

So...why does it work with the hard coded value and not a string variable?

推荐答案

理论上没有错,我可以看到

There is nothing wrong in theory i can see

更新

在sqlServer上为我工作正常

This is working fine for me on sqlServer

public IQueryable<Account> DoesNotReturnAnyData(MyEntities db,string searchText) {
    return (from a in db.Accounts
        where a.accountname.Contains(searchText )
        select a)
}

这篇关于MySQL Linq使用.Contains(variable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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