Group and count in linq C# - c#

I have a query that needs to retrieve 3 fields:
| MaintenanceID | MaintenanceIDCount | StatusID |
| 1 | 2 | -1 |
| 3 | 2 | -1 |
The field MaintenanceIDCount (like the name says), is the count of MaintenanceID column.
My basic query expression is above:
var result = from m in Maintenance
select new
{
m.MaintenanceID,
m.StatusID
}
The result of this query is:
| MaintenanceID | StatusID |
| 1 | -1 |
| 1 | -1 |
| 3 | -1 |
| 3 | -1 |
How can I group and mount my query to retrieve a column with the MaintenanceID column count?
Some tips?

from m in Maintenance
group m by new { m.MaintenanceID, m.StatusID } into g
select new {
g.Key.MaintenanceID,
g.Key.StatusID,
MaintenanceIDCount = g.Count()
}

Related

C# Datatable - group by multiple columns with linq

I have a Datatable like this
| Supplier | Product | Price1 | Price2 | Price3 | ... | PriceN |
|-----------|---------|--------|--------|--------|-----|--------|
| Supplier1 | Orange | 100 | 105 | 150 | ... | 180 |
| Supplier1 | Orange | 110 | 130 | 140 | ... | 180 |
| Supplier2 | Orange | 200 | 250 | 270 | ... | 350 |
| Supplier2 | Orange | 250 | 270 | 320 | ... | 270 |
I want to group rows as next:
| Supplier | Product | Price1 | Price2 | Price3 | ... | PriceN |
|-----------|---------|---------|---------|---------|-----|---------|
| Supplier1 | Orange | 100-110 | 105-130 | 140-150 | ... | 180 |
| Supplier2 | Orange | 200-250 | 250-270 | 270-320 | ... | 270-350 |
Count of columns like "PriceN" can be arbitrary.
How can I do this with LINQ?
You can group by Supplier and Product as
var result = from x in data
group x by new { x.Supplier, x.Product }
select x;
or
var result = data.GroupBy(x => new { x.Supplier, x.Product });
similarly you can use any number of property in group clause
you have to group by it separately in another lambda expression
result.tolist().GroupBy(p=> p.x,p.x2,p.x3 ...);
You can use GroupBy and JOIN to concatenate the values of the price columns:
var groupedSupplier = supplier.GroupBy(s => new { s.Supplier, s.Product })
.Select(supplier => supplier.Supplier,
supplier.Product,
supplier.Price1 = string.Join(",", supplier.Select(x => x.Price1)),
supplier.Price2 = string.Join(",", supplier.Select(x => x.Price2)),
...);
To group by multiple columns in lambda expression use:
var groupedSupplier = supplier.GroupBy(s => s.Supplier,
s => s.Product)
For details, please see chapter 11 of "C# in Depth" by Jon Skeet, Chapter 11 "Query expressions and LINQ to Objects: 11.6 Groupings and continuations", http://csharpindepth.com/

Linq .GroupBy() with count

I have a table that I need to summarize in a report. This is my sample table.
Orders
_____________________________________
CustomerId | CustomerName | OrderType
___________|______________|__________
1 | Adam | Shoe
1 | Adam | Shoe
1 | Adam | Shoe
1 | Adam | Hat
1 | Adam | Hat
2 | Bill | Shoe
2 | Bill | Hat
3 | Carl | Sock
3 | Carl | Hat
I am trying to summarize this to pass back in my viewmodel without a loop. This is the result that I am attempting to achieve.
CustomerName | Shoe | Hat | Sock | Total Orders
------------ | ---- | --- | ---- | ------------
Adam | 3 | 2 | 0 | 5
Bill | 1 | 1 | 0 | 2
Carl | 0 | 1 | 1 | 2
//var resultList = dbContext.Orders.OrderBy(o => o.CustomerId);
How can I use GroupBy and Count to achieve my desired results? Would that be the best approach to take?
group clause (C# Reference)
var summary = from order in dbContext.Orders
group order by order.CustomerId into g
select new {
CustomerName = g.First().CustomerName ,
Shoe = g.Count(s => s.OrderType == "Shoe"),
Hat = g.Count(s => s.OrderType == "Hat"),
Sock = g.Count(s => s.OrderType == "Sock"),
TotalOrders = g.Count()
};
if items are fixed:
public List<OrderViewModel> GetCustOrders()
{
var query = orders
.GroupBy(c => c.CustomerName)
.Select(o => new OrderViewModel{
CustomerName = o.Key,
Shoe = o.Where(c => c.OrderType == "Shoe").Count(c => c.CustomerId),
Hat = o.Where(c => c.OrderType == "Hat").Count(c => c.CustomerId),
Sock = o.Where(c => c.OrderType == "Sock").Count(c => c.CustomerId),
Total = o.Count(c => c.CustomerId)
});
return query;
}
use SQL is one option, i tested it and get exactly what you want:
select p.*, t.total as 'Total Orders' from
(
select CustomerName, count(CustomerId) total from Orders group by CustomerName
) as t inner join
(
select * from Orders
pivot(count(CustomerId)
for OrderType in ([Shoe], [Hat], [Sock])
) as piv
)as p on p.CustomerName = t.CustomerName

Join a multiple tables get clone only demand not supply

Plantdemand
Id | FId | FY
------------------
22 | 1 | 2011-15
No.PlantDemand
Id | PDId | CId | Demand
------------------------
1 | 22 | 1 | 100
2 | 22 | 2 | 200
3 | 22 | 3 | 300
^
"- Id of plantDemand
PlantSupply
Id | FId | DId | FY
---------------------
11 | 1 | 22 | 2012-13
^
"-Id of plantDemand
No.PlantSuply
ID | PSId | CId | Supply
---------------------------
1 | 11 | 1 | 10
2 | 11 | 2 | 10
^
"--Id of PlantSupply
I am stuck to get a CId entries not in table No.PlantSupply of FId=1 like to get a clone who demand enter but not supplied
var getNoofPlantDemand = (from r in getdemand
join nd in context.tbl_NoOfPlantDemanded on r.PlantDemandId equals nd.PlantDemandId into list1
from l1 in list1.DefaultIfEmpty()
join p in context.PlantationTypes on r.PlantationTypeId equals p.Id into list3
from l3 in list3.DefaultIfEmpty()
select new
{}).toList()
var getCloneDemand = (from r in getNoofPlantDemand
join cl in context.Clones on r.CloneId equals cl.Id into list4
from l4 in list4.DefaultIfEmpty()
var getPlantSupply = (from r in getCloneDemand
join s in context.PlantSupply on r.PlantDemandId equals s.DemandId into list
from l1 in list.DefaultIfEmpty()
join ns in context.No.PlantSupply on l1.Id equals ns.PSId into list1
from l2 in list1.DefaultIfEmpty()
where r.CloneId != l2.CloneId && r.PlantDemandId==l1.DemandId && r.PlantationTypeId == l2.PlantationTypeId
select new
{}).toList()
my requirement:
Id | FId | FY | CId | Demand
22 | 1 | 2011-012 | 3 | 300
Please let me know if anybody know how can i get the clone those are not supplied only a demand entry

Join 2 datatable on difference columns

I have 2 DataTable. I want to use LINQ to join the 2 datatable on difference columns. How to do that?
Table A:
+--------+-------+-------+
| ACol1 | ACol2 | ACol3 |
+--------+-------+-------+
| 1 | tbA12 | tbA13 |
| 2 | tbA22 | tbA23 |
| 3 | tbA32 | tbA33 |
| 4 | tbA42 | tbA43 |
| 5 | tbA52 | tbA53 |
+--------+-------+-------+
Table B:
+-------+-------+-------+
| BCol1 | BCol2 | BCol3 |
+-------+-------+-------+
| 1 | XX | tbB13 |
| XX | 1 | tbB23 |
| XX | 2 | tbB33 |
| 4 | XX | tbB43 |
+-------+-------+-------+
SQL Query:
SELECT a.*, b.BCol3
FROM tableA a
JOIN tableB b ON a.ACol1=b.BCol1 OR a.ACol1=b.BCol2
Expected Result:
+--------+-------+-------+-------+
| ACol1 | ACol2 | ACol3 | BCol3 |
+--------+-------+-------+-------+
| 1 | tbA12 | tbA13 | tbB13 |
| 1 | tbA12 | tbA13 | tbB23 |
| 2 | tbA22 | tbA23 | tbB33 |
| 4 | tbA42 | tbA43 | tbB43 |
+--------+-------+-------+-------+
Currently my LINQ query are below:
var query1= from rowA in tableA.AsEnumerable()
join rowB in tableB.AsEnumerable()
on rowA["ACol1"].ToString() equals rowB["BCol1"].ToString()
select new
{
rowA["ACol1"],
rowA["ACol2"],
rowA["ACol3"],
rowB["BCol3"]
};
var query2= from rowA in tableA.AsEnumerable()
join rowB in tableB.AsEnumerable()
on rowA["ACol1"].ToString() equals rowB["BCol2"].ToString()
{
rowA["ACol1"],
rowA["ACol2"],
rowA["ACol3"],
rowB["BCol3"]
};
var result=query1.Union(query2);
Any better idea how to solve this?
LINQ support for JOIN with non-trivial conditions is very limited. You could do a cross join + move your condition to where clause.
var query1= from rowA in tableA.AsEnumerable()
from rowB in tableB.AsEnumerable()
where rowA["ACol1"].ToString() == rowB["BCol1"].ToString()
|| rowA["ACol1"].ToString() == rowB["BCol2"].ToString()
select new
{
rowA["ACol1"],
rowA["ACol2"],
rowA["ACol3"],
rowB["BCol3"]
};
Try this:-
var result = from a in tableA.AsEnumerable()
from b in tableB.AsEnumerable()
where a.Field<string>("ACol1") == b.Field<string>("BCol1")
|| a.Field<string>("ACol1") == b.Field<string>("BCol2")
select new
{
a["ACol1"],
a["ACol2"],
a["ACol3"],
b["BCol3"]
};
Here is the complete working Fiddle, you can copy paste the same in your editor and test because its not supporting AsEnumerable in DotNetFiddle.

How to take the difference of two sums in two tables in mysql

I have two tables to treat in a query for getting the product in hand.
ProdBiscuit
+----+-----+-------+---------------+
| ID | QTY |StockID| Name |
+----+-----+-------+---------------+
| 1 | 100 | 1 | Bis Chocolat |
+----+-----+-------+---------------+
| 2 | 120 | 2 | Bis Moutarde |
+----+-----+-------+---------------+
| 3 | 100 | 3 | Bis Epice |
+----+-----+-------+---------------+
StockData
+----+-----+-------+------+----+
| ID | QTY |ProdID |Status|Type|
+----+-----+-------+------+----+
| 1 | 100 | 1 | 0 | 3 |
+----+-----+-------+------+----+
| 2 | 120 | 2 | 0 | 3 |
+----+-----+-------+------+----+
| 3 | 100 | 3 | 0 | 3 |
+----+-----+-------+------+----
| 4 | 200 | 1 | 0 | 4 |
+----+-----+-------+------+----+
| 5 | 200 | 2 | 0 | 4 |
+----+-----+-------+------+----+
| 6 | 48 | 1 | 0 | 2 |
+----+-----+-------+------+----+
| 7 | 96 | 2 | 4 | 3 |
+----+-----+-------+------+----+
| 8 | 200 | 1 | 4 | 4
+----+-----+-------+------+----+
ProdBiscuit ID is relational with StockData.ProdID
ProdBiscuit.StockId is relational with StockData.ID
StockData.Status = 0 => Entry in stock, > 0 =>Left the stock
Type means 2=>Macaron, 3=>Biscuit, 4=>Ganache
ProdID => ID of ProdBiscuit
StockID-> ID of StockData
With the method below I get this
+--------------+--------+---------+
|Name |QTY PROD|QTY REST |
+--------------+--------+---------+
|Bis Chocolat | 100 | 100 |
+--------------+--------+---------+
|Bis Moutarde | 120 | 24 |
+--------------+--------+---------+
|Bis Epice | 100 | 100 |
+--------------+--------+---------+
Result is correct but the query seems to me to be incorrect.
Because in WHERE clause query search the rows where status > 0
However I get the correct result??
Here is my code :
private void RefreshProd()
{
Int16 ProdQty;
Int16 UsedQty;
DateLst.Items.Clear();
ProdLst.Items.Clear();
QtyLst.Items.Clear();
NomLst.Items.Clear();
prodidLst.Items.Clear();
using (MySqlConnection conn = new MySqlConnection(PublicVariables.cs))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT tb.id, tb.qty,tb.nom, tb.proddate, IF(status > 0 AND tb.id = sd.prodid, sd.quantite, 0) AS rq FROM StockData AS sd, " + TableName + " AS tb WHERE sd.matcuisine = 3 AND sd.status > 0", conn))
{
conn.Open();
try
{
MySqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
DateLst.Items.Add(Convert.ToDateTime(r["proddate"]).ToString("d"));
ProdQty = Convert.ToInt16(r["qty"]);
UsedQty = Convert.ToInt16(r["rq"]);
ProdLst.Items.Add(r["qty"]).ToString();
NomLst.Items.Add(r["nom"]);
prodidLst.Items.Add(r["id"]).ToString();
QtyLst.Items.Add((ProdQty-UsedQty).ToString());
}
}
catch (MySqlException e)
{
MessageBox.Show("MYSQL ERROR--> " + e.ToString());
}
}
}
}
I could not find the rub.
The thing that's astonishing me that the query seems to me to be faulty but gives a correct result.
In where clause I put a condition that the search must be "sd.status>0" but it seems to me it's fetching the status = ZERO conditions too.(Perhaps I am missing something technical in MYSQL WHERE clause)
How ever bu doing my premenade overthestackflow I learnt that UNION and JOIN is more powerfull then the method that I use.
Is there a more efficient way to write my query by JOIN oro UNION? I will appreciate your help. Because I couldn't find way to resolve it by this way.
this query?
SELECT
tb.id
, tb.qty
, tb.nom
, tb.proddate
, sd.quantity as rq
FROM
StockData AS sd
INNER JOIN " + TableName + " AS tb
On tb.id = sd.prodid
WHERE
sd.matcuisine = 3
AND sd.status > 0

Categories