C # 3.0 Getting Started series (11)-in, like operation

Source: Internet
Author: User
Tags what sql

There is an example of how to find a field in a table that is somewhere in a given collection. SQL is simple to write, for example: Select * from table where ID in (2,3, 4, 5). is to look for the value of the ID field within this given set (2,3, 4, 5). So what does LINQ to SQL do? A word, simple.

In Operator

For example, we want to find orders for three customers, "Arout", "Bolid" and "Fissa". How do you do that? This is done by Linq to SQL.

string[] customerID_Set = new string[] { "AROUT", "BOLID", "FISSA" };

var q = (from o in db.Orders
where customerID_Set.Contains(o.CustomerID)
select o).ToList();

The SQL statement that it generates is

SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [
t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[Sh
ipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPosta
lCode], [t0].[ShipCountry]
FROM [dbo].[Orders] AS [t0]
WHERE [t0].[CustomerID] IN (@p0, @p1, @p2)
-- @p0: Input String (Size = 5; Prec = 0; Scale = 0) [AROUT]
-- @p1: Input String (Size = 5; Prec = 0; Scale = 0) [BOLID]
-- @p2: Input String (Size = 5; Prec = 0; Scale = 0) [FISSA]

The first definition of an array, in LINQ query, is also very well understood by using contains, which is that this array contains all the CustomerID, that is, all the CustomerID in the return result. which is in. You can also put the definition of an array in a LINQ statement. Like what:

var q = (from o in db.Orders
where (new string[] { "AROUT", "BOLID", "FISSA" }).Contains(o.CustomerID)
select o).ToList();

Not in? Plus the reverse is

var q2 = (from o in db.Orders
where !(new string[] { "AROUT", "BOLID", "FISSA" }).Contains(o.CustomerID)
select o).ToList();

It's as simple as that.

Like Operator

Like operation, a bit as in, but the direction changed. What does that mean. Is that you give a string to find a field in the data that contains the string. Is that the given string is a subset of a field. That's what SQL script says.

Selec * from table where id like '%AD%'
Selec * from table where id like '%AD'
Selec * from table where id like 'AD%'

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.