Linq Select Objects in List Where Exists in (A,B,C)

Linq select objects in list where exists IN (A,B,C)

Your status-codes are also a collection, so use Contains:

var allowedStatus = new[]{ "A", "B", "C" };
var filteredOrders = orders.Order.Where(o => allowedStatus.Contains(o.StatusCode));

or in query syntax:

var filteredOrders = from order in orders.Order
where allowedStatus.Contains(order.StatusCode)
select order;

Linq: select objects where list contains properties that must be in another list

var result = allDetails.Where(detail => detail
.programs.All(x => SpecificPrograms.Contains(x.ID)));

LINQ Query: Determining if object in one list exists in another based on key

http://introducinglinq.com/blogs/marcorusso/archive/2008/01/14/the-not-in-clause-in-linq-to-sql.aspx

Consider this code that returns all the customers who don't have an order in the Orders table. This is one SQL query that returns that value.

SELECT *
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[CustomerID] NOT IN (
SELECT [t1].[CustomerID]
FROM [dbo].[Orders] AS [t1]
)

This is not the faster way to get the desired result (using a NOT EXISTS is the favorite way - more on this shortly). LINQ offers a Contains extension method that allows writing the following code.

NorthwindDataContext dc = new NorthwindDataContext();
dc.Log = Console.Out;
var query =
from c in dc.Customers
where !(from o in dc.Orders
select o.CustomerID)
.Contains(c.CustomerID)
select c;
foreach (var c in query) Console.WriteLine( c );

In LINQ to SQL the query is translated into this SQL code:

SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName],
[t0].[ContactTitle], [t0].[Address], [t0].[City],
[t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
FROM [dbo].[Customers] AS [t0]
WHERE NOT (EXISTS(
SELECT NULL AS [EMPTY]
FROM [dbo].[Orders] AS [t1]
WHERE [t1].[CustomerID] = [t0].[CustomerID]
))

This approach is not only semantically equivalent, but also faster in execution. The following is the result with SET STATISTICS IO ON. The first result is for the hand-written query that use the NOT IN clause. The second result is for the LINQ to SQL generated query.

LINQ where in List

AllProjects = AllProjects.Where(x=>projects.Contains(x.ProjectID))

You just need to check if the projects list contains the id you are looking for

Linq Intermediate Object in method chain for Where and Select?

If you use LINQ with query syntax, you have the let keyword that creates a temporary to use later in the query. When the query syntax is translated by the compiler into fluent/lambda syntax, the let is translated into a Select that bundles the temporary values with any values you need to carry into future methods.

You can do the same manually:

var rowData = registersRows
.Select(r => new { RowIndex = r.RowIndex, cells = r.Elements<Cell>().ToList() })
.Select(rc => new { rc.RowIndex, rc.cells, A = GetCellText(rc.cells, "A", rc.RowIndex, sharedStringTableItems) })
.Where(rca => included.Contains(rca.A))
.Select(rca => new RegistersRow {
StoreNumber = rca.A,
ChannelName = GetCellText(rca.cells, "D", rca.RowIndex, sharedStringTableItems),
ChannelDisplayName = GetCellText(rca.cells, "E", rca.RowIndex, sharedStringTableItems),
PhysicalDeviceName = GetCellText(rca.cells, "F", rca.RowIndex, sharedStringTableItems),
FriendlyName = GetCellText(rca.cells, "G", rca.RowIndex, sharedStringTableItems),
DisplayNameInLabel = GetCellText(rca.cells, "H", rca.RowIndex, sharedStringTableItems),
NumberOfRegisters =
int.Parse(GetCellText(rca.cells, "K", rca.RowIndex, sharedStringTableItems))
})
.ToList();

Use LINQ to get items in one List, that are not in another List

This can be addressed using the following LINQ expression:

var result = peopleList2.Where(p => !peopleList1.Any(p2 => p2.ID == p.ID));

An alternate way of expressing this via LINQ, which some developers find more readable:

var result = peopleList2.Where(p => peopleList1.All(p2 => p2.ID != p.ID));

Warning: As noted in the comments, these approaches mandate an O(n*m) operation. That may be fine, but could introduce performance issues, and especially if the data set is quite large. If this doesn't satisfy your performance requirements, you may need to evaluate other options. Since the stated requirement is for a solution in LINQ, however, those options aren't explored here. As always, evaluate any approach against the performance requirements your project might have.

Linq select one of list where one of its child objects properties matches a value

Found the answer.

match.Scores.FirstOrDefault(x => x.Team.Competitors.Any(u => u.UserId == User.UserId)).Points.Value;

So this basically says.

Get me the points property for the first score.

that has a team that contains a competitor that userId is equal to the User objects Id



Related Topics



Leave a reply



Submit