English 中文(简体)
LIKEs and ORs and enuff in Linq
原标题:LIKEs and ORs and stuff in Linq

I m试图与LKE/Contains合用LL.KE/Contains合用L.K.的手法。

<>SQL:

SELECT * FROM Users
WHERE GroupNumber =  A123456 
OR (FirstName LIKE  Bob%  AND LastName LIKE  Smith% )

这将使“博卜·史密斯”等名字的所有人,以及拥有一个集团的所有人,其人数完全等于A123456。 在我的数据库中,图表示例给我三项成果(预期成果):

A123456   John Davis
A312345   Bob Smith
A123456   Matt Jones

Linq: (provided PNum = A123456; first = "Bob"; last = "Smith")

var users = from a in dc.Users
        where a.PolicyNumber == PNum || (SqlMethods.Like(a.FirstName, first + "%") && SqlMethods.Like(a.LastName, last + "%"))
        orderby a.PolicyNumber, a.FirstName
        select a;

这只会给我带来休斯敦左边的结果:

A123456   John Davis
A123456   Matt Jones

我也尝试了“Contains()”和“StartsWith()”,但每个版本的我都取得了相同的结果。 当我去除任何类似/内容/内容时,我取得了理想的结果,但我需要抛弃。 在Linq的询问中,我如何取得所有三个成果?

最佳回答

在此情况下,我肯定会使用<条码>StartsWith,这只是使法典在阅读时更像C#,但这项工作应当:

var users = from a in dc.Users
        where a.PolicyNumber == PNum 
              || (a.FirstName.StartsWith(first) && a.LastName.StartsWith(last))
        orderby a.PolicyNumber, a.FirstName
        select a;

如果问询工作不成功,你能否把它产生的“结构”摆在桌面上? 仅仅确定背景就可将其写给青少年,或者说最简单。 (我将写一本仅用来检验这一问题的灯光——它比每时都安装一台自动识别器容易。)

问题回答

你的问询确实令我正确。

你们是否试图看一下这些记录,看看一下哪一种情况?





相关问题
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: ...

热门标签