i have this LINQ query
var data = (from orders in db.Orders
group orders by orders.OrderDate into dateGroup
select new OrderDateGroup()
{
OrderDate = dateGroup.Key,
OrderCount = dateGroup.Count()
}).Take(10);
orders.OrderDate is DateTime but i want to group only on the basis of Date
You can also use Into Group with GroupBy in VB.Net. LINQ query is ended with the help Select or Groupby clause. It can also support method syntax in both C# and VB.Net languages. As shown in example 2.
TruncateTime(Nullable<DateTime>) When used as part of a LINQ to Entities query, this method invokes the canonical TruncateTime EDM function to return the given date with the time portion cleared.
The Let keyword allows you to create a range variable and initialized with the result of the query expression and then you are allowed to use that variable with the upcoming clause in the same query.
In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, . NET collections, and any other format for which a LINQ provider is available.
You can use canonical functions:
group orders by EntityFunctions.TruncateTime(orders.OrderDate) into dateGroup
Additional info:
EntityFunctions
methods are called canonical functions. And these are a set of functions, which are supported by all Entity Framework providers. These canonical functions will be translated to the corresponding data source functionality for the provider. Canonical functions are the preferred way to access functionality outside the core language, because they keep the queries portable.
You can find all canonical functions here and all Date and Time Canonical Functions here.
Also, don't forget to include System.Data.Objects
namepsace from System.Data.Entity.dll
.
Note:
If you are using EF6, then you must consider to change EntityFunctions
to DbFunctions
. And don't forget to include System.Data.Entity
namepsace from EntityFramework.dll
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With