Linq select top one related entity by date năm 2024

For example, you deserialize a JSON file into a list of Customer with the

var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 0 and a few other properties populated. Then you want to retrieve those customers from the database to update those properties.

A frequent solution is using the

var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 1 methods such as:

var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; It works great and is easy to use.

However, this solution has some limitations such as:

  • It only supports basic types like var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 2 or var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 3
  • The list of var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 4 is limited due to SQL limitations
  • It doesn't support surrogate key [more than one key] or other complex scenarios

The WhereBulkContains method lets you filter a query by including all entities from the list. It's doesn't have any of the

var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 1 method limitations.

FAQ

How to use the method WhereBulkContains?

The most basic scenario is passing a list to the WhereBulkContains method.

The WhereBulkContains method will filter entities to include those contained in the list.

var customers = context.Customers.WhereBulkContains[deserializedCustomers]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, x => x.Code]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, new List { "Code" }]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, "Code"]; Try it

The

var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 9 method is also supported.

What kind of list is supported?

All kinds of lists are supported. The only requirement is that your list is a basic type or contains a property with the same name as the key:

  • Basic type such as var customers = context.Customers.WhereBulkContains[deserializedCustomers]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, x => x.Code]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, new List { "Code" }]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, "Code"]; 0 and var customers = context.Customers.WhereBulkContains[deserializedCustomers]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, x => x.Code]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, new List { "Code" }]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, "Code"]; 1
  • Entity Type such as var customers = context.Customers.WhereBulkContains[deserializedCustomers]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, x => x.Code]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, new List { "Code" }]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, "Code"]; 2
  • Anonymous Type
  • Expando Object list

{ var ids = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.WhereBulkContains[ids]; } { var customers = context.Customers.WhereBulkContains[deserializedCustomers]; } { var anonymousIds = deserializedCustomers.Select[x => new { x.CustomerID }].ToList[]; var customers = context.Customers.WhereBulkContains[anonymousIds]; } { var expandos = new List[]; deserializedCustomers.ForEach[x => {

dynamic expando = new ExpandoObject[];
expando.CustomerID = x.CustomerID;
expandos.Add[expando];
}]; var customers = context.Customers.WhereBulkContains[expandos]; } Try it

The

var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 9 method is also supported.

Can I use WhereBulkContains after a Where?

Yes, the WhereBulkContains is an extension method that you can chain like any other LINQ method. You can chain it before or after the

var customers = context.Customers.WhereBulkContains[deserializedCustomers]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, x => x.Code]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, new List { "Code" }]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, "Code"]; 5 or any other LINQ methods.

var customers = context.Customers.Where[x => x.CustomerID >= 2].WhereBulkContains[deserializedCustomers]; Try it

Can I use the WhereBulkContains with million of items?

Yes, you can use the WhereBulkContains method with an unlimited amount of items.

Under the hood, we create a temporary table and populated it with our very fast BulkInsert method. Then we use this temporary table to perform an

var customers = context.Customers.WhereBulkContains[deserializedCustomers]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, x => x.Code]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, new List { "Code" }]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, "Code"]; 7 statement.

How can I use a custom key?

By default, we create the join using the entity key, but you can choose a custom key with one or many properties by passing:

  • A lambda expression
  • A list of string

var customers = context.Customers.WhereBulkContains[deserializedCustomers, x => x.Code]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, new List { "Code" }]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, "Code"]; Try it

Can I use WhereBulkContains with Batch Operations?

The WhereBulkContains method is compatible with our batch methods:

  • UpdateFromQuery
  • DeleteFromQuery
  • InsertFromQuery

context.Customers.WhereBulkContains[deserializedCustomers].UpdateFromQuery[x => new { FirstName = "UpdateFromQuery" }]; context.Customers.WhereBulkNotContains[deserializedCustomers].DeleteFromQuery[]; context.Customers.WhereBulkContains[deserializedCustomers].InsertFromQuery[x => new { x.Code, FirstName = "Copied", x.LastName, x.Email }]; Try it

What are Contains method limitations?

Has said previously, the

var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 1 method already work great but also have his own limitations:

  • It only supports basic types like var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 2 or var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 3
  • The list of var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 4 is limited due to SQL limitations
  • It doesn't support surrogate key [more than one key] or other complex scenarios

The WhereBulkContains doesn't have any of those limitations.

Do WhereBulkContains faster than Contains method?

In most scenarios, the answer will probably be no. The

var customerIds = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.Where[x => customerIds.Contains[x.CustomerID]].ToList[]; 1 method is faster due to simply using a very basic

{ var ids = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.WhereBulkContains[ids]; } { var customers = context.Customers.WhereBulkContains[deserializedCustomers]; } { var anonymousIds = deserializedCustomers.Select[x => new { x.CustomerID }].ToList[]; var customers = context.Customers.WhereBulkContains[anonymousIds]; } { var expandos = new List[]; deserializedCustomers.ForEach[x => {

dynamic expando = new ExpandoObject[];
expando.CustomerID = x.CustomerID;
expandos.Add[expando];
}]; var customers = context.Customers.WhereBulkContains[expandos]; } 5 statement.

The WhereBulkContains method is also very fast, but the main advantage is his flexibility by supporting:

  • An unlimited amount of items
  • Any kind of list
  • Custom key/surrogate key

What is the difference between the method WhereBulkContains, WhereBulkNotContains, and BulkRead?

The

{ var ids = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.WhereBulkContains[ids]; } { var customers = context.Customers.WhereBulkContains[deserializedCustomers]; } { var anonymousIds = deserializedCustomers.Select[x => new { x.CustomerID }].ToList[]; var customers = context.Customers.WhereBulkContains[anonymousIds]; } { var expandos = new List[]; deserializedCustomers.ForEach[x => {

dynamic expando = new ExpandoObject[];
expando.CustomerID = x.CustomerID;
expandos.Add[expando];
}]; var customers = context.Customers.WhereBulkContains[expandos]; } 7 method is similar to the WhereBulkContains method, but it filters entities not contained [

{ var ids = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.WhereBulkContains[ids]; } { var customers = context.Customers.WhereBulkContains[deserializedCustomers]; } { var anonymousIds = deserializedCustomers.Select[x => new { x.CustomerID }].ToList[]; var customers = context.Customers.WhereBulkContains[anonymousIds]; } { var expandos = new List[]; deserializedCustomers.ForEach[x => {

dynamic expando = new ExpandoObject[];
expando.CustomerID = x.CustomerID;
expandos.Add[expando];
}]; var customers = context.Customers.WhereBulkContains[expandos]; }

  1. instead of contained [

var customers = context.Customers.Where[x => x.CustomerID >= 2].WhereBulkContains[deserializedCustomers]; 0]:

  • The WhereBulkContains method filters entities to include entities from the list [ var customers = context.Customers.WhereBulkContains[deserializedCustomers]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, x => x.Code]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, new List { "Code" }]; var customers = context.Customers.WhereBulkContains[deserializedCustomers, "Code"]; 7 statement]
  • The { var ids = deserializedCustomers.Select[x => x.CustomerID].ToList[]; var customers = context.Customers.WhereBulkContains[ids]; } { var customers = context.Customers.WhereBulkContains[deserializedCustomers]; } { var anonymousIds = deserializedCustomers.Select[x => new { x.CustomerID }].ToList[]; var customers = context.Customers.WhereBulkContains[anonymousIds]; } { var expandos = new List[]; deserializedCustomers.ForEach[x => {
    dynamic expando = new ExpandoObject[];  
    expando.CustomerID = x.CustomerID;  
    expandos.Add[expando];  
    
    }]; var customers = context.Customers.WhereBulkContains[expandos]; } 7 method filters entities to exclude entities from the list [ var customers = context.Customers.Where[x => x.CustomerID >= 2].WhereBulkContains[deserializedCustomers]; 4 statement].

As for the

var customers = context.Customers.Where[x => x.CustomerID >= 2].WhereBulkContains[deserializedCustomers]; 5 method, under the hood calls the WhereBulkContains method followed by the

var customers = context.Customers.Where[x => x.CustomerID >= 2].WhereBulkContains[deserializedCustomers]; 7 or

var customers = context.Customers.Where[x => x.CustomerID >= 2].WhereBulkContains[deserializedCustomers]; 8 method.

How do I select top 5 records in LINQ?

Linq Query To Select Top 5 Records.

var list = [from t in ctn.Items..

where t.DeliverySelection == true && t.Delivery.SentForDelivery == null..

orderby t.Delivery.SubmissionDate..

select t]. Take[5];.

What is the difference between first [] and single [] extension methods in LINQ?

Single[] asserts that one and only one element exists in the sequence. First[] simply gives you the first one. When to use? Use Single / SingleOrDefault[] when you are sure there is only one record present in a database or you can say if you querying on a database with the help of the primary key of the table.

How to select certain elements in a list using LINQ methods?

In LINQ queries, the Where operator is used to select certain elements from a sequence. It expects an expression that evaluates to a boolean value. Every element satisfying the condition will be included in the resulting query.

How to add days to a date in LINQ query?

AddDays[Nullable, Nullable] When used as part of a LINQ to Entities query, this method invokes the canonical AddDays EDM function to add the given number of days to a date/time.

Chủ Đề