In this article, we will learn to use LINQ group by DateTime and sum using C#. We have already learned group by in SQL query.
I’m presuming that you are knowledgeable with SQL queries and group by clause If not then please go through the below articles:

I have a list of products purchased on a specific date. I want to group them all into one date sold on the same day and then sum them Qty and CostPrice.Basically, we want to group the item based on the Month and Year of purchase date.

Order Table

ProductName Qty CostPrice ShipingCost SoldDate
Computers 5 800 20 01/15/2022 0:00
Sports 3 500 20 01/15/2022 0:00
Cameras 2 400 20 01/15/2022 0:00
furniture 4 250 20 02/24/2022 0:00
Sports 6 500 20 03/24/2022 0:00
furniture 6 250 20 03/26/2022 0:00
Cameras 8 400 20 04/26/2022 0:00
Computers 9 800 20 04/27/2022 0:00

Output

SoldDate ProductName Qty CostPrice ShipingCost
Jan-22 Computers 5 800 20
Jan-22 Sports 3 500 20
Jan-22 Cameras 2 400 20
Feb-22 furniture 4 250 20
Mar-22 Sports 6 500 20
Mar-22 furniture 6 250 20
Apr-22 Cameras 8 400 20
Apr-22 Computers 9 800 20

I created a products class which looks like the below:

public class Product
    {
        public string Name { get; set; }
        public int Qty { get; set; }
        public int CostPrice { get; set; }
        public int ShipingCost { get; set; }
        public DateTime SoldDate { get; set; }
        public List<Product> GetProducts()
        {
            var objList = new List<Product>();
            objList.Add(new Product() { Name = "Computers", Qty = 5, CostPrice = 800, ShipingCost = 20, SoldDate = new DateTime(2022, 01, 15) });
            objList.Add(new Product() { Name = "Sports", Qty = 3, CostPrice = 500, ShipingCost = 20, SoldDate = new DateTime(2022, 01, 15) });
            objList.Add(new Product() { Name = "Cameras", Qty = 2, CostPrice = 400, ShipingCost = 20, SoldDate = new DateTime(2022, 01, 15) });
            objList.Add(new Product() { Name = "furniture", Qty = 4, CostPrice = 250, ShipingCost = 20, SoldDate = new DateTime(2022, 02, 24) });
            objList.Add(new Product() { Name = "Sports", Qty = 6, CostPrice = 500, ShipingCost = 20, SoldDate = new DateTime(2022, 03, 24) });
            objList.Add(new Product() { Name = "furniture", Qty = 6, CostPrice = 250, ShipingCost = 20, SoldDate = new DateTime(2022, 03, 26) });
            objList.Add(new Product() { Name = "Cameras", Qty = 8, CostPrice = 400, ShipingCost = 20, SoldDate = new DateTime(2022, 04, 26) });
            objList.Add(new Product() { Name = "Computers", Qty = 9, CostPrice = 800, ShipingCost = 20, SoldDate = new DateTime(2022, 04, 27) });
            return objList;
        }
    }

Linq Query

 

var products = new Product().GetProducts();
            var results = from p in products
                          let k = new
                          {
                              MonthYear = p.SoldDate.ToString("MMM-yy"),
                              Product = p.Name
                          }
                          group p by k into t
                          select new
                          {
                              MonthYear = t.Key.MonthYear,
                              Product = t.Key.Product,
                              Qty = t.Sum(p => p.Qty),
                              CostPrice = t.Sum(p => p.CostPrice),
                              ShipingCost = t.Sum(p => p.ShipingCost)
                          };
            foreach (var r in results)
            {
                Debug.Print(($@"SoldDate:""{r.MonthYear}"",Product:""{r.Product}"",Qty:""{r.Qty}"",CostPrice:""{r.CostPrice}"",ShipingCost:""{r.ShipingCost}"""));
            }

*If you have any query ,please comment,Thank you

Bunch By section is utilized to put every one of the columns with a similar worth. These qualities are of that predefined section in one gathering. It connotes that all lines will put an equivalent sum through a solitary section, which is of one suitable segment in one gathering.

In LINQ, The Group By articulation is utilized for getting sorted out comparative information into gatherings. The information is additionally coordinated with the assistance of a comparable capacity. That is to say, that assuming that various lines in an exact segment have similar qualities, it will organize those columns in a gathering.

The total capacities permit you to play out the estimation of a bunch of lines and return a solitary worth. The GROUP BY proviso is frequently utilized with a total capacity to perform estimations and return a solitary incentive for every subgroup.

For instance, if you need to know the quantity of orders in every status, you can utilize the COUNT work with the GROUP BY statement as follows:

By and large, DISTINCT proviso is an extraordinary instance of the GROUP BY condition. The contrast between DISTINCT proviso and GROUP BY condition is that the GROUP BY statement sorts the outcome set, while the DISTINCT proviso doesn’t.

 

The post [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax appeared first on Software Development | Programming Tutorials.



Read More Articles