Get unique row with many conditions - c#

I have a history table of ordered products
+----+------------+--------+
| id | IdProduct | status |
+----+------------+--------+
| 1 | 100 | 1 |
| 2 | 100 | 2 |
| 3 | 100 | 3 |
| | | |
| 4 | 200 | 1 |
| 5 | 200 | 2 |
| | | |
| 6 | 300 | 1 |
| 7 | 300 | 2 |
+----+------------+--------+
I want to get only the products who have the status 2 but not 3
+----+------------+
| id | IdProduct |
+----+------------+
| 5 | 200 |
| | |
| 7 | 300 |
+----+------------+
How can I achieve this using a Linq request

Using linq to sql you can do:
var result = history.GroupBy(item => item.IdProduct)
.Where(grp => grp.Any(item => item.Status == 2) &&
!grp.Any(item => item.Status == 3))
.Select(grp => new {
IdProduct = grp.Key,
Id = grp.Max(item => item.Id)
});
Or:
var result = history.GroupBy(item => item.IdProduct)
.Where(grp => grp.Any(item => item.Status == 2) &&
!grp.Any(item => item.Status == 3))
.Select(grp => grp.Where(item => ite.Status == 2).FirstOrDefault());
In your case both these should return the same because the max(id) correleted with the result you wanted
If you know each status exists only once then you can try the following. The idea is that status 3 items equal -1, 2 equals 1 and the rest 0. Only groups that have status 2 but not 3 will have the result of 1
var result = history.Select(item => new { Item = item, Valid = item.Status == 2 ? 1 : item.Status == 3 ? -1 : 0 })
.GroupBy(item => item.Item.IdProduct)
.Where(grp => grp.Sum(item => item.Valid) == 1)
.Select(item => item.Item);

I didnt read the last line in question How can I achieve this using a Linq request may be that reason for downvote..
Will keep this answer if someone is looking to solve it in SQL
Here is one way using Group by and Having clause
SELECT *
FROM yourtable
WHERE IdProduct IN (SELECT IdProduct
FROM Yourtable
GROUP BY IdProduct
HAVING Count(CASE WHEN status = 3 THEN 1 END) = 0
AND Count(CASE WHEN status = 2 THEN 1 END) > 0)
AND Status = 2
Count(CASE WHEN status = 3 THEN 1 END) = 0
This condition is to make sure status = 3 does not exist any row for each ID
Count(CASE WHEN status = 2 THEN 1 END) > 0
This condition is to make sure alteast one row with status = 2 for each ID

Declare #YourTable table (id int,IdProduct int,status int)
Insert Into #YourTable values
( 1 , 100 , 1 ),
( 2 , 100 , 2 ),
( 3 , 100 , 3 ),
( 4 , 200 , 1 ),
( 5 , 200 , 2 ),
( 6 , 300 , 1 ),
( 7 , 300 , 2 )
Select Id,IdProduct
From (
Select Id
,IdProduct
,MaxStatus = max(Status) over (Partition By IdProduct)
From #YourTable
Where Status in (2,3)
) A
Where MaxStatus = 2
Returns
Id IdProduct
5 200
7 300

Related

How do I merge two count queries from one table?

The first query :
Id | UserId | projectId |date | Status
1 | 1 | 1 | 2020 | PENDDING
2 | 1 | 2 | 2020 | DONE
3 | 2 | 1 | 2020 | PENDDING
And what I tried two queries :
the first is about to get all userwork with X project for example id = 1
var FirstQery = context.table1.where (C => C.ProjectId == 1).count();
The second query is to fetch the number of user with project x have "done"status
var SecondQery = context.table1.where (C => C.ProjectId == 1 && C.Status == "DONE").count();
I want return object have only two values : countNumberUserWithXProject
and countNumberUserByXProjectHaveXStatus
It is known approach with fake grouping.
var query =
from t in context.table1
where t.ProjectId == 1
group t by 1 into g
select new
{
Count = g.Count(),
DoneCount = g.Sum(x => x.Status == "DONE" ? 1 : 0)
}
var result = query.FirstOrDefault();
How about returning an anonymous object (or if you like it more a typized object like a int[]).
return new {count1 = FirstQery , count2 = SecondQery };
(Return, or assign like var result = new {count1....} etc
You can also replace the FirstQuery and SecondQuery directly with the Linq query.

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

Group and count in linq 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()
}

Join with count and multiple conditions - LINQ C#

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();

Linq query summary by column with ordering

The scenario is like this
public class Test {
public string name;
public int val1;
public int val1;
}
name |val 1 |val 2|
'aa' | 10 | 4 |
'aa' | 30 | 5 |
'bb' | 14 | 4 |
'bb' | 16 | 6 |
'cc' | 5 | 5 |
'cc' | 2 | 1 |
'cc' | 1 | 1 |
What is the best way group by name and get summary val_1 ans val_2 for every name
as
name |val 1 |val 2|
'aa' | 40 | 9 |
'bb' | 30 | 10 |
'cc' | 8 | 7 |
Try this
var results =
from t in db.Tests
group t by t.name into g
orderby g.Key
select new
{
name = g.Key,
val_1 = g.Sum(x => x.val_1),
val_2 = g.Sum(x => x.val_2)
};
Or if you prefer fluent syntax:
var results = db.Tests.GroupBy(t => t.name)
.OrderBy(g => g.Key)
.Select(g => new
{
name = g.Key,
val_1 = g.Sum(x => x.val_1),
val_2 = g.Sum(x => x.val_2)
});

Categories