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
Related
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
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.
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()
}
I have a property database and I am trying to get all properties added by an user. The main table is called 'Property' and there are other tables which are 'PropertyPhotos', 'City' etc. A sample database is as follows:
'Property' table
PropertyId| Area| State| UserId | ...
1 | 1 | 1 | AAA | ...
2 | 2 | 3 | BBB | ...
3 | 1 | 1 | AAA | ...
'PropertyPhotos'
PropertyPhotoId| PropertyId| FileName | MainPic
1 | 1 | x1.jpg | 1
2 | 1 | X2.jpg | 0
3 | 2 | x3.jpg | 1
4 | 3 | x4.jpg | 1
5 | 3 | x5.jpg | 0
6 | 3 | x6.jpg | 0
'AreaLookUp'
AreaLookUpId | AreaDescription
1 | London
2 | Birmingham
3 | Manchester
I am trying to write a LINQ query to get information on property added by a particular user. But I am stuck when trying to retrieve the 'FileName' of the MainPic and also get count. See code below with comments.
So, for the data above, this query should return the following for "UserId = AAA"
PropertyId | ... | MainPicSrc | PhotoCount
1 | ... | x1.jpg | 2
3 | ... | xr4jpg | 3
Please help!
public IEnumerable<PropertyExcerptViewModel> GetAddedPropertyVmByUserId(string userId)
{
var addedProperties = from p in db.Property where p.UserId == userId
join pp in db.PropertyPhotos on p.PropertyId equals pp.PropertyId
join a in db.AreaLookUp on p.Area equals a.AreaLookUpId
select new PropertyExcerptViewModel
{
PropertyId = p.PropertyId,
PropertyType = p.PropertyType,
TransactionType = p.TransactionType,
IsPropertyDisabled = p.IsPropertyDisabled,
IsPropertyVerified = p.IsPropertyVerified,
IsPropertyNotified = p.IsPropertyNotified,
MainPicSrc = pp.FileName, // How to put where condition to only get FileName of just the Main Pic
PhotoCount = pp.Count(), // How to get count of all pics with a particular proprtyId
Price = p.Price,
NoOfBedrooms = p.NoOfBedrooms,
Area = a.AreaLookUpDescription,
ShortDescription = (p.Description.Length > 300) ? p.Description.Substring(0,300) : p.Description
};
return addedProperties.ToList();
}
I think where statement might be easier if you care about clear match
var data=(from c in db.Property from v in db.PropertyPhotos from
n in db.AreaLookUpId
where c.PropertyId==v.PropertyId && c.Area==n.AreaLookUpId && c.UserId=="AAA"
// the rest is your select
PhotoCount = v.Where(j=>j. PropertyId==c.PropertyId).Count()
This also works - I ended up doing it this way
var addedProperties = from p in db.Property
join ppic in db.PropertyPhotos on p.PropertyId equals ppic.PropertyId into pp
join a in db.AreaLookUp on p.Area equals a.AreaLookUpId
join cal in db.CalendarEvent on p.PropertyId equals cal.PropertyId into c
where p.UserId == userId
select new PropertyExcerptViewModel
{
PropertyId = p.PropertyId,
PropertyType = p.PropertyType,
PropertyCategoryDescription = pc.PropertyCategoryDescription,
TransactionType = p.TransactionType,
IsPropertyDisabled = p.IsPropertyDisabled,
IsPropertyVerified = p.IsPropertyVerified,
IsPropertyNotified = p.IsPropertyNotified,
MainPicSrc = pp.Where(e => e.MainPic == true).FirstOrDefault().PhotoLocation,
PhotosCount = pp.Count(),
Price = p.Price,
NoOfBedrooms = p.NoOfBedrooms,
Area = a.AreaLookUpDescription,
ShortDescription = (p.Description.Length > 300) ? p.Description.Substring(0, 300) : p.Description,
LatestCalendarEvent = c.OrderByDescending(e => e.DateSaved).FirstOrDefault()
};
return addedProperties.ToList();
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