I need to order a list by status == "Rejected" and date ascending and then by status == "Accepted" and date descending.
I am trying the following but I'm not sure how to go about this:
items
.OrderBy(x => x.status == "Rejected")
.ThenBy(x => x.DateSubmitted)
.ThenBy(x => x.status == "Accepted")
.ThenByDescending(x => x.DateSubmitted)
You're describing the task a bit awkward, which (I think) leads you to a slightly wrong implementation.
I say:
items
.Where(x => x.status == "Rejected")
.OrderBy(x => x.DateSubmitted)
.Concat(items.
.Where(x => x.status == "Accepted")
.OrderByDescending(x => x.DateSubmitted));
The main difference is that now, status other than "Rejected" or "Accepted" are not shown.
However, I think it is what you intended. If you want the full set, consider using
items
.Where(x => x.status == "Rejected")
.OrderBy(x => x.DateSubmitted)
.Concat(items.
.Where(x => x.status != "Rejected")
.OrderByDescending(x => x.DateSubmitted));
PS. This is also assuming Linq-To-Objects. I'm not very well-versed with Linq-to-EF or Linq-to-SQL
Related
I have the following code:
var test = _fitDbContext.MvLatestTestResult
.Include(r => r.LastTest)
.Include(r => r.LastTest.Testrun)
.Include(r => r.LastTest.Testrunconfig)
.Where(r => r.LastTest.Testrunconfig.Started.Value.Hour >= 0 && r.LastTest.Testrunconfig.Started.Value.Hour < 6)
.Where(r => r.LastTest.Testrun.Userc == "build")
.Where(r => r.ProductId == productId)
.GroupBy(r => r.LastTest.Testrunconfig.Started.Value.Date)
.OrderByDescending(r => r.Key.Date)
.Take(3)
.ToDictionary(group => group.Key, group => group.GroupBy(r => r.Configfilename));
Variable test looks like this:
You can see that LastTest is null. If I remove .OrderByDescending(r => r.Key.Date), it contains value.
Why does OrderByDescending remove LastTest value?
EDIT
Interesting thing I just found out. If I change order of GroupBy and OrderBy, everything works as expected. The problem is that OrderBy is executed with all records and takes a long time.
var test = _fitDbContext.MvLatestTestResult
.Include(r => r.LastTest)
.Include(r => r.LastTest.Testrun)
.Include(r => r.LastTest.Testrunconfig)
.Where(r => r.LastTest.Testrunconfig.Started.Value.Hour >= 0 && r.LastTest.Testrunconfig.Started.Value.Hour < 6)
.Where(r => r.LastTest.Testrun.Userc == "build")
.Where(r => r.ProductId == productId)
.OrderByDescending(r => r.Key.Date)
.GroupBy(r => r.LastTest.Testrunconfig.Started.Value.Date)
.Take(3)
.ToDictionary(group => group.Key, group => group.GroupBy(r => r.Configfilename));
I want to be able to sort by d.DateUpdated but only when it's not DateTime.MinValue. If it's DateTime.MinValue then I would need it to be able to sort by d.DateCreated. It there a way to do this with in the following line of code?
return database.Table<Announcement>()
.Where(i => i.GroupId == groupId)
.OrderByDescending(d => d.DateUpdated)
.ToListAsync();
You might as well use ThenBy method
return database.Table<Announcement>()
.Where(i => i.GroupId == groupId)
.OrderByDescending(d => d.DateUpdated)
.ThenByDescending(d => d.DateCreated)
.ToListAsync();
Something like that?
return database.Table<Announcement>()
.Where(i => i.GroupId == groupId)
.OrderByDescending(d => d.DateUpdated == null ? d.DateCreated : d.DateUpdated)
.ToListAsync();
I have below stated 2 tables:
now I want to get the set of Child Table objects for whichever their parent table entries are latest(wr.r.t lastmodified). It should be something like....
List<Child_Table> List = ChildsList.Where(x=>x.name =="pqr" && status == "done")
.Select(x=>x.Parent.lastmodified == recent record).....ToList();
You can use GroupBy on the date, then OrderByDescending on the Key then take the First followed by SelectMany to flatten the results.
var result = ChildsList.Where(x => x.name == "pqr" && x.status == "done")
.GroupBy(x => x.Parent.lastmodified)
.OrderByDescending(g => g.Key)
.First()
.SelectMany(g => g)
.ToList();
You could use a join to accomplish it:
var results = children
.Join(parents.OrderByDescending(p => p.lastmodified).Take(1),
c => c.parent_id,
p => p.id,
(c, p) => c)
.Where(x => x.name == "pqr" && x.status == "done")
.ToList();
I'm not sure how to do this with the entity framework. I got the following:
return this.enrollments
.Where(e => e.em_enrolled == false && e.em_result < _settings.PassMark)
.GroupBy(e => e.em_subject_id)
.Select(e => e.em_subject_id)
.ToList();
how do i only retrieve records that are present x times.
Do you mean groups with x or more items?
return this.enrollments
.Where(e => e.em_enrolled == false && e.em_result < _settings.PassMark)
.GroupBy(e => e.em_subject_id)
.Where(g => g.Count() >= x)
.Select(g => g.Key)
.ToList();
I suspect you want:
return this.enrollments
.Where(e => !e.em_enrolled && e.em_result < _settings.PassMark)
.GroupBy(e => e.em_subject_id)
.Where(g => g.Count() >= x)
.Select(g => g.Key)
.ToList();
Note that I've changed the Select part to reflect the fact that you want to extract the group key from the group. (I've also avoided comparison with false, changing e.em_enrolled == false into !e.em_enrolled. They mean the same thing of course, but I find the latter more idiomatic in C#.)
I am trying to order a list of products based on the zindex property of the cross reference table with the category table (in this case called 'Chassis'), but I get the following error:
Cannot order by type 'System.Collections.Generic.IEnumerable`1[System.Int32]'.
The following is the method I am using:
public IQueryable<E_Product> Product_GetList_ByChassisId(int chassisId)
{
return dc.E_Products
.Where(x => x.Deleted == false)
.Where(x => x.Published == true)
.Where(x => x.E_Product_Chassis
.Any(c => c.ChassisId == chassisId && c.Deleted == false))
.OrderBy(x => x.E_Product_Chassis.Select(c => c.Zindex));
}
I understand the .Select method returns an IEnumerable, but being a many-to-many relationship, x.E_Product_Chassis does not allow simple selection of its properties (e.g. x.E_Product_Chassis.Zindex).
Any help would be very appreciated...
FirstOrDefault(), Min(), Max() -- use one of these functions to select the appropriate z-index out of the set.
public IQueryable<E_Product> Product_GetList_ByChassisId(int chassisId)
{
return dc.E_Products
.Where(x => x.Deleted == false)
.Where(x => x.Published == true)
.Where(x => x.E_Product_Chassis
.Any(c => c.ChassisId == chassisId && c.Deleted == false))
.OrderBy(x => x.E_Product_Chassis.Min(c => c.Zindex));
}