mvc Groupby and Orderby nested - c#

mvc beginner
I have a table of lots that contain a property Num_of_steps representing the number of completed steps toward building a house.
I currently use this to retrieve the lot information and am sorting by the lot number.
var ViewModel = new Sub_lot_VM();
ViewModel.Subdivisions = db.Subdivisions
.Include(i => i.Lots)
.ToList();
if (ViewModel.Subdivisions !=null) // if data sort by lot number
{
foreach (var item in ViewModel.Subdivisions)
item.Lots = item.Lots.OrderBy(i => i.LotName).ToList();
}
return View(ViewModel);
}
Now I want to display this information a 3 groups:
first where the count is between 1 and 114 (active),
second where the count is above 115 (or GTE 115?) (finished)( and then orderby lot name) and
third group is count = 0 (not started) also order by lotname.
I've been trying to think of how to add .where and .groupby lambda expressions to my method without luck. Such as.where(I=>i.Lot.Num_of_steps=0).
I also see that I needed a foreach where some LINQ examples did not need the foreach. Still confused on that.

Get the lots first and then use groupby with ranges to get the groups
from x in
(
db.Subdivisions.SelectMany(sd => sd.Lots)
)
group x by x.Num_of_steps == 0 ? 3 : x.Num_of_steps < 115 ? 1 : 2 into g
orderby g.Key
select g.OrderBy(g1 => g1.LotName)
You can give the groups meaningful names in stead of 1, 2 and 3, but you can also postpone that until it's display time. The numbers facilitate correct sorting.

Related

Add a Lambda in a LINQ query to replace a line in a foreach

I have a situation where I have to match up multiple customers numbers from one system with a single customer number in another system.
So for instance customer number 225, 228 and 223 in system A will all map to customer number 110022 in system B.
Easy enough, I have a matrix setup to do that.
I pull the matrix data in like this:
 var dt_th_matrix = (from m in aDb.Matrix_Datatrac_TopHat select m).ToArray();
So the records would be something like:
customerA: 3 CustomerB: 1001
CustomerA: 4 CustomerB: 1001
CustomerA: 5 Customer: 1002
Then I do a big data pull and step through all the items. For each of the items I go grab the matching customer number from the matrix like this:
foreach (var dt_stop in mainPull)
{
int? th_customerId = (from d in dt_th_matrix
where d.datatrac_customer_no == dt_stop.Customer_No.ToString()
select d.tophat_customer_detail_Id).First();
What I would rather do is to just embed the code to grab the customer numbrer from the matrix directly in my datapull -- the part "Query goes here somehow" will be some type of Lambda I assume. Any help?
I have tried something like this:
th_customerId = (dt_th_matrix.First().tophat_customer_detail_Id.Equals c.Customer_No)
But that is not it (obviously)
var mainPull = (from c in cDb.DistributionStopInformations
join rh in cDb.DistributionRouteHeaders on c.Route_Code equals rh.Route_Code
where c.Company_No == 1 &&
(accountNumbers.Contains(c.Customer_No)) &&
(brancheSearchList.Contains(c.Branch_Id) && brancheSearchList.Contains(rh.Branch_Id)) &&
c.Shipment_Type == "D" &&
(c.Datetime_Created > dateToSearch || c.Datetime_Updated > dateToSearch) &&
rh.Company_No == 1 &&
((rh.Route_Date == routeDateToSearch && c.Route_Date == routeDateToSearch) ||
(rh.Route_Date == routeDateToSearch.AddDays(1) && c.Route_Date == routeDateToSearch.AddDays(1)))
orderby c.Unique_Id_No
select new
{
c.Datetime_Updated,
th_customerId = ("Query goes here somehow")
c.Datetime_Created,
c.Unique_Id_No,
c.Original_Unique_Id_No,
c.Unique_Id_Of_New_Stop,
c.Branch_Id,
c.Route_Date,
c.Route_Code,
c.Sequence_Code,
c.Customer_No,
c.Customer_Reference,
c.Shipment_Type,
c.Stop_Name,
c.Stop_Address,
c.Stop_City,
c.Stop_State,
c.Stop_Zip_Postal_Code,
c.Stop_Phone_No,
c.Stop_Arrival_Time,
c.Stop_Departure_Time,
c.Address_Point,
c.Stop_Special_Instruction1,
c.Stop_Special_Instruction2,
c.Stop_Expected_Pieces,
c.Stop_Expected_Weight,
c.Stop_Signature,
c.Actual_Arrival_Time,
c.Actual_Depart_Time,
c.Actual_Service_Date,
c.Stop_Actual_Pieces,
c.Stop_Exception_Code,
c.Created_By,
rh_Route_Date = rh.Route_Date,
routeHeaderRouteCode = rh.Route_Code,
rh.Actual_Driver,
rh.Assigned_Driver,
rh_routeDate = rh.Route_Date
}).ToArray();
I will try and clarify the above.
What I need is for the Linq query to say :
For each record that I pull I will goto the Array named dt_th_matrix and get the record that matches for this line and use it.
The data in the matrix looks exactly like this:
Record 1: datatrac_customer_no: 227, tophat_customer_detail_Id 1
Record 2: datatrac_customer_no: 228, tophat_customer_detail_Id: 1
Record 3: datatrac_customer_no: 910, tophat_customer_detail_Id: 5
Then for the first record pulled in the mainPull the field c.customer_no == 228 so I need the query in the select new statement to replace th_customerId with 1 (from Record 2 in the Matrix.
Then say the next record pulled in the mainPull the field c.customer_no = 910 the th_customerId would be 5.
That is what the first line of my foreach statement is currently doing. I want to move that logic to inside my LINQ query.
If I understand you correctly, using a dictionary with a key of datatrac_customer_no and a value of tophat_customer_detail_Id would be a good idea here:
var dt_th_matrix = (from m in aDb.Matrix_Datatrac_TopHat select m).ToDictionary(m=>m.datatrac_customer_no,m=>m.tophat_customer_detail_Id);
With this you should be able to replace your "Query goes here somehow" with
dt_th_matrix[c.Customer_No]
Using LINQ would be possible as well, but I don't think it's worth the performance overhead and reduction in readibility.
If you still want to use LINQ for this with your original matrix, this should work as your query:
dt_th_matrix.Single(m => m.datatrac_customer_no == c.Customer_No).tophat_customer_detail_Id
Both expressions will throw an exception if the key is not found or exists multiple times - but if I understand your structure correctly this should not be possible. Otherwise you need to check for this.

Efficient way of finding multiple dates per ID

I'm trying to query my MsSQL Express database to find all CompanyID's which have multiple dates associated - when I say multiple dates, I must point out they need to be over different days.
EG
ID UkDate CompanyId
1 01/01/2015 16
2 01/01/2015 16
3 03/01/2015 18
4 05/01/2015 19
5 06/01/2015 20
6 08/01/2015 20
In the example above, only the rows with ComapnyID 20 would be returned because it occurred multiple times and those times were over dates (note that although companyId 16 has multiple entries, but both entries are the same date).
I'm not sure how to write the query for this using Linq. My object is already IQueryable<T> but, I'm not sure how to perform the query without executing the code, and then 'finishing off' the query.
I'm not near Visual Studio but the code would be (please forgive typing errors, this is from memory)
//First, grab unique CompanyIds as this removes those who didn't visit multiple times
var uniqueIds = (from d in this._database.MyTable
select companyId).Distinct();
//This is the problem because on each iteration I'm re-querying the database!
foreach(var id in uniqueIds)
{
var result = (from d in this._database.MyTable.OrderBy(a=>a.UkDate)
where d.CompanyId==id
select d);
//check for nulls
if (result.First(a=>a.UkDate.Day) != result.Last(a => a.UkDate.Day)
{
this.AllResultsList.AddRange(results);
}
}
Whilst it works without error I don't feel the code is correct - it feels like a hack and unefficient but this was my best effort. Is there a way I could reduce the number of database requests I make and achieve the same result
It would be something along the lines of
var results = myTable.GroupBy(x => x.CompanyID)
.Where(g => g.GroupBy(g2 => g2.UkDate).Count()>1)
.Select(g => g.Key);
Live example (albeit with LinqToObjects, but the query should work against a database just fine): http://rextester.com/FPHI53553
var results = (from o in this._database.MyTable
group o by o.CompanyId into grouped
where (grouped.Max(s => s.UKDate) - grouped.Min(s => s.UKDate)).TotalDays > 0
select grouped.Key);
Edit (by OP)
Final result:
var results = (from o in this._database.MyTable
group o by o.CompanyId into grouped
where (Convert.ToDateTime(grouped.Max(s => s.UKDate)) - Convert.ToDateTime(grouped.Min(s => s.UKDate))).TotalDays > 0
from l in myTable
where l.CompanyID == grouped.Key
select l).ToList();
A little different version:
var result = (from o in this._database.MyTable
group o by o.CompanyId into grouped
select new {
grouped.Key,
Count = grouped.Select(c => c.UkDate).Distinct().Count()
} into filter
where filter.Count > 1
join a in this._database.MyTable on filter.Key equals a.CompanyID
select new { a.CompanyID, a.UkDate}
).ToList();
You can also try this if you want the company id and a count of the different dates:
from c in dataTable
group c by c.CompanyId into grouped
let count = grouped.Select(x => x.UkDate).Distinct().Count()
where count > 1
select new { CompanyId = grouped.Key, Count = count }

Linq lambda expression many to many table select

I have three tables, which two of them are in many to many relationship.
Picture:
This is the data in middle mm table:
Edit:
Got until here, I get proper 4 rows back, but they are all the same result(I know I need 4 rows back, but there are different results)
return this._mediaBugEntityDB.LotteryOffers
.Find(lotteryOfferId).LotteryDrawDates
.Join(this._mediaBugEntityDB.Lotteries, ldd => ldd.LotteryId, lot => lot.Id, (ldd, lot) =>
new Lottery
{
Name = lot.Name,
CreatedBy = lot.CreatedBy,
ModifiedOn = lot.ModifiedOn
}).AsQueryable();
My question is, how can I retrieve all the Lotteries via many to many table WHERE I have LotteryOfferId given only?
What I want to achieve is to get data from lottery table by LotteryDrawDateId.
First I use LotteryOfferId to get DrawDates from middle table, and by middle table I get drawDateIds to use them in LotteryDrawDate table. From that table I need to retreive Lottey table by LotteryId in LotteryDrawDate table.
I gain this by normal SQL(LotteryOffersLotteryDrawDates is middle table in DB, not seen in model):
select
Name, Lotteries.CreatedBy, Lotteries.ModifiedOn, count(Lotteries.Id)
as TotalDrawDates from Lotteries join LotteryDrawDates on Lotteries.Id
= LotteryDrawDates.LotteryId join LotteryOffersLotteryDrawDates on LotteryDrawDates.Id =
LotteryOffersLotteryDrawDates.LotteryDrawDate_Id
where LotteryOffersLotteryDrawDates.LotteryOffer_Id = 19 group by
Name, Lotteries.CreatedBy, Lotteries.ModifiedOn
But Linq is different story :P
I would like to do this with lambda expressions.
Thanks
db.LotteryOffer.Where(lo => lo.Id == <lotteryOfferId>)
.SelectMany(lo => lo.LotteryDrawDates)
.Select( ldd => ldd.Lottery )
.GroupBy( l => new { l.Name, l.CreatedBy, l.ModifiedOn } )
.Select( g => new
{
g.Key.Name,
g.Key.CreatedBy,
g.Key.ModifiedOn,
TotalDrawDates = g.Count()
} );
You can do this:
var query = from lo in this._mediaBugEntityDB.LotteryOffers
where lo.lotteryOfferId == lotteryOfferId
from ld in lo.LotteryDrawDates
group ld by ld.Lottery into grp
select grp.Key;
I do this in query syntax, because (in my opinion) it is easier to see what happens. The main point is the grouping by Lottery, because you get a number of LotteryDrawDates any of which can have the same Lottery.
If you want to display the counts of LotteryDrawDates per Lottery it's better to take a different approach:
from lot in this._mediaBugEntityDB.Lotteries.Include(x => x.LotteryDrawDates)
where lot.LotteryDrawDates
.Any(ld => ld.LotteryDrawDates
.Any(lo => lo.lotteryOfferId == lotteryOfferId))
select lot
Now you get Lottery objects with their LotteryDrawDates collections loaded, so afterwards you can access lottery.LotteryDrawDates.Count() without lazy loading exceptions.

How to get unique value with LINQ>SQL?

Using LINQ to SQL, how do I get the row with 1, 21? I'm looking for
SomeId==1
and
SecondId is a unique entry
SomeId SecondId
0 20
1 21
1 22
1 22
EDIT:
Ok, sorry. That wasn't clear. What I'm trying to do is generically find that row. There might be another entry that looks like this:
1 25
And that is the only 25. So I would get back two rows. Without referencing specific Ids, how do I find these two rows?
EDIT: Okay, it was really unclear what you meant before, but now I think I see what you mean, and you want something like:
var query = from row in table
where row.SomeId == targetId
group row by row.SecondId into g
where g.Count() == 1
select g.Single();
In other words:
Filter by SomeId first
Group by SecondId
Filter so that only groups with a single entry for that SecondId are returned
Select the sole entry from that group
There can be multiple such groups, of course - so you would get (1, 21) and (1, 25) in your example.
EDIT: If you saying you would like to find any combination of SomeId & SecondId where there are more than one row for that combination? then you could do the following:
var results = source.Where(x => x.SomeId == 1).GroupBy(x => x.SecondId).Where(g => g.Count > 1);
This will give you groups of results, and only return those that have more than one row. So in your example, you would get a group that returns 1,22...
If you are looking for the case where you only have rows in which there is a single entry in the table with that combination (the opposite of what I'm returning) you can change the comparison operator from '>' to '==' and another answer-er has also shown this possibility.

Return Top X Results From Linq To Objects Query in Order

PREVIOUSLY: In this question someone told me how to use CompareTo to return surnames within a particular range ordered alphabetically using LINQ to Objects.
The rest of my question, which seems to have missed the initial question asking feeding frenzy, actually arose after I tested this solution. In the previous eaxample I had a list of names:
Adams
Bentham
Bickford
Gillies
Kelly
Moore
Peters
Rutherford
Smith
Taylor
Williams
And I wanted to be able to query them for all the names between Gillies and Moore for example and get:
Gillies
Kelly
Moore
This is all well and good if you want every single solitary name inbetween the goalposts returned no matter what. The problem comes in when you have a huge quantity of surnames and you want a maximum of four of the names between Gillies and Taylor returned in alphabetical order, for example.
So the desired output is:
Gillies
Kelly
Moore
Peters
However just returning four results between Gillies and Taylor could return Kelly, Peters, Smith and Taylor, or Gillies, Moore, Rutherford and Smith. Basically, the query takes you at your word and just selects any old four between the goalposts.
So, how can I get the top 4 results alphabetically. I could write a second query of course and return a subset and then select from within that... but shouldn't there be a way to integrate this behaviour into the initial query?
I've tried a couple of things with OrderBy and so far they just don't work. So it's over to you guys.
EDIT: for those two of you who've suggested using "take" I already am using take. It doesn't "take" in order, even if you use OrderBy or at least it doesn't in my query. Here it is:
var allIDs = (from cust in dc.orders
join item in dc.order_items on cust.orderid equals item.orderid
join del in dc.deliveries on cust.deliveryid equals del.deliveryid
join dt in dc.deliverytypes on del.deliverytype equals dt.deliverytypeid
where eventcode == item.itemcode
&& dt.description == "Secure Post"
&& (cust.status == "OK" || cust.status == "PB")
&& cust.surname.CompareTo(surfrom ?? " ") >= 0
&& cust.surname.CompareTo(surto ?? "zzz") <= 0
&& (cust.trackingcode == null ? false : (bool)cust.trackingcode)==false
orderby cust.surname, cust.initials, cust.ordercode
select cust.orderid).Distinct().Take(ordermax);
That just returns four names from between the names you've selected, not a specific four names.
From your edit it looks like you're doing the orderby and distinct in a strange order:
This works for me (where "allMyNames" is just a List<string>).
var ofInterest = allMyNames
.Distinct()
.Where(x => x.CompareTo(from) >= 0 && x.CompareTo(to) <= 0)
.OrderBy(x => x)
.Take(4);
I'm much happier using the extension form of LINQ :)
Use the "Take" LINQ method to take the first 4 records:
var query = (from name in originalList
where name.CompareTo(fromName) >= 0 && name.CompareTo(toName) <= 0
orderby name
select name).Take(4);
GetNames(...).Take(4);
Will take the first four items in the enumerable.

Categories

Resources