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%'