Lambda expression with where clause, how to do a group by - c#

I have a lambda expression which returns the records I'm looking for, except that the names are not distinct, so I need to group by name, here is what I have, can someone give me a hand with this.
var cities = _context.country.OrderBy(c => c.Name)
.Where(c => c.Feature_Code != featureCode
&& c.Feature_Code != featureCode2
&& c.Country_Code == CountryCode
&& c.Admin1_code == StateId)
.Select(c => new CityViewModel
{
CityId = c.CountryID,
CityName = c.Name
}
).AsQueryable();

You could use:
var cities = _context.country
.Where(c => /*Your where conditions*/)
.GroupBy(c => c.Name)
.OrderBy(grp => grp.Key)
.Select(grp => new CityViewModel
{
CityId = grp.First().CountryID,
CityName = grp.Key
});
OrderBy should be used after Where.

Depending on what you want, you could add:
.GroupBy(c => c.CityName)

You could use:
var cities = _context.country.Distinct(c => c.Name).Where(c => c.Feature_Code != featureCode
&& c.Feature_Code != featureCode2
&& c.Country_Code == CountryCode
&& c.Admin1_code == StateId)
.Select(c => new CityViewModel
{
CityId = c.CountryID,
CityName = c.Name
}
This should return only one instance of each name.

Related

Need to select multiple columns in Entity Framework, but need to get only distinct on single column

I have a database table that has the following records. There are more fields than what I have displayed below, but I only want to return the Owner, Brand Name, ID#, and Type fields.
Owner Ingredient BrandName ID# Type
XXX Methoprene Precor 123-333 H
XXX Permethrin Precor 123-333 H
I am trying to write an Entity Framework query to select only the distinct records in the Brand Name field, but still return the other columns in a list back to the controller to display in a partial view. The following is the code I have attempted, but I cannot seem to get the query written correctly:
return db.Pesticides
.Where(c => c.Owner == Owner && c.BrandName == PesticidesBrand)
.GroupBy(c => c.Owner, c =>c.BrandName )
.Select(d => d.FirstOrDefault())
.ToList();
I realize the Select clause is not correct, but need help getting correct syntax to return the 4 specific fields. I would like the query to return the following record:
XXX Precor 123-333 H
Thanks in advance....
I believe this is what you are looking for.
var record = db.Pesticides
.Where(c => c.Owner == Owner && c.BrandName == PesticidesBrand)
.Select(c => new { c.Owner, c.BrandName, c.ID, c.Type })
.FirstOrDefault();
If you want to return this from a result you need to project it to a known type.
PesticideModel record = db.Pesticides
.Where(c => c.Owner == Owner && c.BrandName == PesticidesBrand)
.Select(c => new PesticideModel{ Owner = c.Owner, BrandName = c.BrandName, ID = c.ID, Type = c.Type })
.FirstOrDefault();
PesticideModel.cs
public class PesticideModel {
public string Owner {get;set;}
public string BrandName {get;set;}
public string ID {get;set;}
public string Type {get;set;}
}
If you wanted to return a list containing a single record do the following:
List<PesticideModel> record = db.Pesticides
.Where(c => c.Owner == Owner && c.BrandName == PesticidesBrand)
.Select(c => new PesticideModel{ Owner = c.Owner, BrandName = c.BrandName, ID = c.ID, Type = c.Type })
.Take(1) // take 1st record
.ToList();
return db.Pesticides
.Where(c => c.Owner == Owner && c.BrandName == PesticidesBrand)
.Select(d => new Pesticide() { Owner = d.Owner, BrandName = d.BrandName, ID = d.ID, Type = d.Type })
.Distinct()
.FirstOrDefault()
.ToList();

Select multiple child columns with the same filter

I have this Linq lambda expression which generates abnormally complex SQL select to database. Is it somehow possibility to simplify it?
var devices = db.Devices
.Where(a => a.active == true)
.Select(a => new DeviceToDisplay
{
Id = a.Id,
serialNumber = a.serialNumber,
deviceRegion = a.deviceRegion,
activeIP = a.IPaddresses.Where(b => b.active == true).Select(b => b.IPaddress1).FirstOrDefault(),
Wip = a.IPaddresses.Where(b => b.active == true).Select(b => b.W_IP).FirstOrDefault(),
Sip = a.IPaddresses.Where(b => b.active == true).Select(b => b.S_IP).FirstOrDefault(),
model = a.SPdatas.Where(c => c.model != "").OrderByDescending(c => c.collectionDate).Select(c => c.model).FirstOrDefault(),
firmware = a.SPdatas.Where(c => c.model != "").OrderByDescending(c => c.collectionDate).Select(c => c.firmware).FirstOrDefault(),
lastMPteamActivity = a.activityLogs.OrderByDescending(c => c.updatedDate).Select(c => c.updatedDate).FirstOrDefault(),
country = a.MPPinformations.Select(c => c.country).FirstOrDefault()
});
For a start, your linq query looks very complicated. Imagine how you would implement this by writing a SQL query for example.
A suggestion: you are writing things like:
a.IPaddresses.Where(b => b.active == true).
and
a.SPdatas.Where(c => c.model != "").OrderByDescending(c => c.collectionDate).
in multiple places.
Instead you could create an anonymous type. For example,
var foo = from x in sb.Devices.Where(a=> a.active)
select new { Id = x.ID,
IPAddress = a.IPaddresses.Where(b => b.active), ... }
You can then use foo to create your Devices object.
See if this is any better:
var devices = db.Devices
.Where(a => a.active == true)
.Select(a => new DeviceToDisplay {
Id = a.Id,
serialNumber = a.serialNumber,
deviceRegion = a.deviceRegion,
activeIP = a.IPaddresses.Where(b => b.active == true).FirstOrDefault(),
SPdata = a.SPdatas.Where(c => c.model != "").OrderByDescending(c => c.collectionDate).FirstOrDefault(),
lastMPteamActivity = a.activityLogs.OrderByDescending(c => c.updatedDate).Select(c => c.updatedDate).FirstOrDefault(),
country = a.MPPinformations.Select(c => c.country).FirstOrDefault()
})
.Select(a=> new DeviceToDisplay {
Id=a.Id,
serialNumber=a.serialNumber,
deviceRegion=a.deviceRegion,
activeIP=a.activeIP.IPaddress1,
Wip=a.activeIP.W_IP,
Sip=a.activeIP.S_IP,
model=a.SPdata.model,
firmware=a.SPdata.firmware,
lastMPteamActivity=a.lastMPteamActivity,
country=a.county
});

Linq - Get Max date from resultset

I need to convert the following SQL query to Linq :-
SELECT CODE,SCODE,MAX(SDATE) AS SDATE FROM SHIFTSCHEDULE
WHERE COMPANY = 'ABC'
GROUP BY CODE,SCODE
ORDER BY MAX(SDATE)
DESC
So far, I have tried this :-
var data = ctx.ShiftSchedule.Where(m =>
m.Company == company && m.EmployeeId == item.EmployeeId
)
.GroupBy(m =>
new
{
m.EmployeeId,
m.ShiftId
})
.Select(m =>
new
{
EmployeeId = m.Key.EmployeeId,
ShiftCode = m.Key.ShiftId,
ShiftDate = m.Max(gg => gg.ShiftDate)
}).ToList();
The results i get are :-
Now what i want is to get record or item in this result set which is MaxDate. In the above image the MaxDate is 1st record.
How to get the MAXDATE from the resultset?
This should work:-
var data = ctx.ShiftSchedule.Where(x => x.Company == company
&& x.EmployeeId == item.EmployeeId)
.GroupBy(x => new { x.CODE, x.SCODE })
.Select(x => new
{
CODE = x.Key.CODE,
SCODE = x.Key.SCODE,
SDATE = x.Max(z => z.SDATE)
})
.OrderByDescending(x => x.SDATE).FirstOrDefault();
You can order the resulting collection and fetch the first object using FirstOrDefault.
If you want just MAXDATE, you can only project that.
Just add .OrderByDescending(x => x.ShiftDate).First(); at the end.
OrderByDescending date and then take .First()
var data = ctx.ShiftSchedule.Where(m =>
m.Company == company && m.EmployeeId == item.EmployeeId
)
.GroupBy(m =>
new
{
m.EmployeeId,
m.ShiftId
})
.Select(m =>
new
{
EmployeeId = m.Key.EmployeeId,
ShiftCode = m.Key.ShiftId,
ShiftDate = m.Max(gg => gg.ShiftDate)
}).ToList().OrderByDescending(x => x.ShiftDate).First();

Lambda expression Group by in C#

I would like to group my LINQ query by ItemNumber and return the whole table with the total for Quantity.
Example:
ItemNumber - ItemName - Quantity
100 Item1 1
150 Item2 2
100 Item1 2
200 Item3 1
150 Item2 2
Should be:
ItemNumber - ItemName - Quantity
100 Item1 3
150 Item2 4
200 Item3 1
This is the query I am trying to group:
public IQueryable<WebsiteOrderStatus> GetOrderStatusByAccountNumberWithoutDeleted
(string accountNumber)
{
return db.WebsiteOrderStatus
.Where(x => x.AccountNumber == accountNumber && x.LastUpdatedStatus != 1);
}
And my best result so far(this can't compile though):
public IQueryable<IGrouping<Int32?, WebsiteOrderStatus>> lol(string accountNumber)
{
db.WebsiteOrderStatus
.Where(x => x.AccountNumber == accountNumber && x.LastUpdatedStatus != 1)
.GroupBy(g => g.ItemNumber)
.Select(g => new
{
g.Key.ItemNumber,
Column1 = (Int32?)g.Sum(p => p.Quantity)
});
}
EDIT:
Thanks for the replies everyone, I must face it. Theese anonymous types are pretty hard to work with in my opinion, so I found another solution.
I made another method, which sums the quantity of the users items and grouped the first one.
public IQueryable<WebsiteOrderStatus> GetOrderStatusByAccountNumberWithoutDeleted(string accountNumber)
{
return db.WebsiteOrderStatus.Where(x => x.AccountNumber == accountNumber && x.LastUpdatedStatus != 1).GroupBy(x => x.ItemNumber).Select(grp => grp.First());
}
public int GetQuantityOfUsersItem(string accountNumber, string itemNumber)
{
return db.WebsiteOrderStatus.Where(x => x.ItemNumber == itemNumber && x.AccountNumber == accountNumber).Sum(x => x.Quantity);
}
At the page where I have my gridview I did:
var query = websiteOrderStatusRep.GetOrderStatusByAccountNumberWithoutDeleted(AppSession.CurrentLoginTicket.AccountNumber).Select(x => new { x.ItemName, x.ItemNumber, x.FormatName, x.Price, x.Status, x.Levering, Quantity = websiteOrderStatusRep.GetQuantityOfUsersItem(x.AccountNumber, x.ItemNumber)});
public IQueryable<IGrouping<Int32?, WebsiteOrderStatus>> lol(string accountNumber)
{
db.WebsiteOrderStatus
.Where(x => x.AccountNumber == accountNumber && x.LastUpdatedStatus != 1)
.GroupBy(g => g.ItemNumber)
.Select(g => new
{
ItemNumber = g.Key,
ItemName = g.First().ItemName,
Count = g.Sum(item => item.Quantity)
});
}
public IQueryable<OrderStatus > lol(string accountNumber)
{
return db.WebsiteOrderStatus
.Where(x => x.AccountNumber == accountNumber && x.LastUpdatedStatus != 1)
.GroupBy(g => g.ItemNumber)
.Select(g =>
new OrderStatus //This is your custom class, for binding only
{
ItemNumber = g.Key,
ItemName = g.First().ItemName,
Quantity = g.Sum(g => g.Quantity)
}
);
}
I think the Select should be:
.Select(g => new
{
ItemNumber = g.Key,
Column1 = (Int32?)g.Sum(p => p.Quantity)
});
Note the change in the first line of the anonymous type. The key of the grouping is already the item number.
The only problems I see with your query are
Missing return statement as per comments
The select statement should be:
-
.Select(g => new {
ItemNumber = g.Key,
Total = g.Sum(p => p.Quantity)
});
EDIT: If you want to get, lets say ItemNumber and ItemName , in the resulting object, you must also group on those fields
db.WebsiteOrderStatus
.Where(x => x.AccountNumber == accountNumber && x.LastUpdatedStatus != 1)
.GroupBy(g => new { g.ItemNumber, g.ItemName })
.Select(g => new
{
ItemNumber = g.Key.ItemNumber,
ItemName = g.Key.ItemName,
Count = g.Sum(item => item.Quantity)
});
You cannot use anonymous type for return value type. So you will never compile the code.
Also your linq expression has IQueryable< [anonymous type] > result type.
I believe that you can do something like this:
public IQueryable<OrderStatus> lol(string accountNumber)
{
db.WebsiteOrderStatus
.Where(order => order.AccountNumber == accountNumber && order.LastUpdatedStatus != 1)
.GroupBy(order => order.ItemNumber)
.Select(grouping => new OrderStatus //This is your custom class, for binding only
{
ItemNumber = grouping.Key,
ItemName = grouping.First().ItemName,
Quantity = grouping.Sum(order => order.Quantity)
});
}
I`ve fixed my answer too :)

Linq return first date

I have the query:
var q = db.tblArcadeGamePlays
.Where(c => c.GameID == GameID && c.ReferalID != 0 && c.ReferalID != null)
.GroupBy(r => new { r.ReferalID })
.Select(g => new { Plays = g.Count(), URL = g.Key.ReferalID })
;
tblArcadeGamePlays also has a field Date, in the group I'd like to return the earliest observed date that group contains.
How would I do this?
.Select(g => new {
Plays = g.Count(), URL = g.Key.ReferalID,
MinDate = g.Min(x => x.Date
});
you might also find the LINQ syntax easier to read/maintain here, but... the extension syntax will work too. But as LINQ:
var q = from c in db.tblArcadeGamePlays
where c.GameID == GameID && c.ReferalID != 0 && c.ReferalID != null
group c by c.ReferalID into g
select new {
Plays = g.Count(), URL = g.Key.ReferalID, MinDate = g.Min(x=>x.Date)
};
this might work
var firstIndex = q.Min(c => c.Date);
var q = db.tblArcadeGamePlays .Where(c => c.GameID == GameID && c.ReferalID != 0 && c.ReferalID != null) .GroupBy(r => new { r.ReferalID }) .Select(g => new { Plays = g.Count(), URL = g.Key.ReferalID , Date = g.Min(cc=>cc.DateField) }) ;
Replace DateField with your actual property

Categories