English 中文(简体)
Linq。 比较价值清单的案文
原标题:Linq.Where-to-SQL on a text field comparing to a list of values

客户是T-SQL DB(我没有控制,因此可能不会改变)类型“文字”的一个领域。

我这样说:

List<string> compare = new List<string>();
compare.Add("one");
compare.Add("two");

var q = from t in customer
        where t.text.Contains( compare.First())
        select t;

这将发挥作用。

但现在我想做的是: NOT WorkingING!

var q = from t in customer
        where compare.Contains( t.text )
        select t;

我如何能够做到这一点? 是否可能?

EDIT:问题显然不清楚: 不能用“=”来提一下KQ中的案文一栏,而只能用LKE。 相比之下。 内容(t.text)将产生错误,因为它被转换成一个使用“=”的查询。

What I did not tell - I thought it is irrelevant - is, that I use LINQ-to-ORM (LLBLGen in this case). What I tried instead:

var q = from t in customer
        where compare.Any( x => t.text.Contains(x) )
        select t;

既然如此,也就不起作用。 目前,我不工作,但例外情况是,不能够转换成《限制令》。

我希望这能澄清一些问题。

EDIT:

Joseph pointed this out to me: PredicateBuilder. It creates an Expression on a given ObjectType. Now my problem is, that my type is an anonymous type out of multiple joins. Is there an easy or elegant way to handle this?

问题回答

现在我可能失踪,但你的法典认为它应该发挥作用。 你们是否包括档案顶点的名称空间?

using System.Linq;
using System.Linq.Expressions;

你们也可以在没有Linq2Sql syntax的情况下改写:

var q = customer.Where(c => compare.Contains(c.text));

您可以利用LinqKit的自由前线建筑商班进行问询。 这里是一个博客员额,介绍其使用情况,并与下载网站链接。

http://thecodeslinger.wordpress.com/2008/10/28/linqkit-predicatebuildert-goodness/

下面是员额的编码样本。

    //First get a list of keywords that match the description entered.
                string[] parts = txtInclude.Text.Split(new[] {‘ ‘});
                string[] noparts = null;
                if(txtButNot.Text.Trim().Length > 0)
                    noparts = txtExclude.Text.Trim().Split(new[] {‘ ‘});

                var pred = PredicateBuilder.True<Pet>();
   //here is where you would loop through your compare object
                parts.ForEach(p => pred = pred.And(pl => pl.description.Contains(p)));
                if(noparts != null)
                    noparts.ForEach(p => pred = pred.And(pl => !pl.description.Contains(p)));

                var pets = from s in db.Pets.Where(pred)
                        select s;

你们必须把案文领域改为说明

        var query = from t in dataContext.table
                    where compare.Contains(t.textField.ToString())
                    select t;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签