Today something strange happened when I was sorting a list using Linq + lambda.
I have a list with articles that should be OrderedByDescending on a Field c => c.Statistics.Created. This field comes back as a DateTime. I do the following:
newsItems = newsItems.OrderByDescending(c => c.Statistics.Created).ToArray<Item>();
I understand that this list will be ordered in a descending direction and that the time of this date is also being watched as this is just a DateTime object. Now the weird part. I adjust the code to look the following:
newsItems = newsItems.OrderByDescending(c => c.Statistics.Created.Date).ToArray<Item>();
Now when I look at the list, the items are still ordered the way they should be. I can't understand why the list is still ordered the right way, when I tell Linq to only look at the date. Somehow Linq decides to look at the time aswell. When I attach my application to the w3p process and watch what happens I see that the Time is actually empty on the Date object, like I expect.
Can someone explain me how this list actually is getting sorted on time when I tell linq to only look at the date?
Already solved!
I have already found what made this strange behavior occur. It was that the list (gotten out of a CMS) was already ordered some way. When I then sorted on the date (with no time) the order was apperently correct an Linq did no do anything else with this date.
Related
I would like to seek someone's help with Business objects Webi reports and how to create a variable that looks at two fields, compares them and returns a specific value.
For example.
I would like the report to look at DateA then look at DateB.(DateB can have many dates) Then return the latest date that comes right before DateA
ID..............DateA.............DateB..............IntendedResults
aaa............1/3/19.............1/6/19..............1/2/19
aaa............1/6/19.............1/4/19..............1/4/19
aaa............1/7/19.............1/2/19..............1/4/19
bbb............1/1/19............1/6/19..............(blank)
bbb............1/9/19.............1/9/19..............(blank)
I created a max for each variable
=Max([DateB]) ForEach([Id]) Where( [DateB]<[DateA])
But I keep returing the wrong results showing 0's and 1's. Please help.
Thank you
I have a daft 3rd party who provide me with multiple items which each contain dates(DateFrom) in string formatted yyyyMMdd.
I am trying to get the soonest date from these using Linq as follows:
_providerRecords.Policy.Min(c => System.DateTime.ParseExact(c.DateFrom, "yyyyMMdd",
CultureInfo.InvariantCulture));
However this keeps returning me the 1st record each time. Can anyone see what the flip I'm doing wrong?
Cheers
Min was working as expected, my late afternoon head caused me the cockup.
It's quite easy to write an XPath or Sitecore Query/Fast query to get all items within a date range:
E.g.
/sitecore/content/*[#__created>='20130301T000000' and #__created<'20130427T000000']
However, this kind of query only looks at the latest version of an item, so it seems impossible to find the actual item's created date (not the version's created date).
I could write a bit of C# code to do the querying but that would involve first retrieving version 1 of every single item in my database before I could then do my filter on created date. This would be mind-bogglingly slow.
Is it possible to do with XPath/Query notation/Fast? If not, is there a way I can do it that will be quick?
I thought of something that might work:
Create a new field which all your items will get. Then in this field, on the creation of an item (not version), you enter the datetime in there.
When versions get deleted, that's fine, because all versions will have that field, with the exact same value.
The only thing is, you'll have to run a script once to loop through your existing items to populate the field with the correct value for each item.
You can then use your XPath query same as now.
I kind of have the feeling this has been asked before, but I have been searching, but cannot come to a clear description.
I have a rails app that holds items that occur on a specific date (like birthdays). Now I would like to make a view that creates a table (or something else, divs are all right as well) that states a specified date once and then iterates over the related items one by one.
Items have a date field and are, of course, not related to a date in a separate table or something.
I can of course query the database for ~30 times (as I want a representation for one months worth of items), but I think it looks ugly and would be massively repetitive. I would like the outcome to look like this (consider it a table with two columns for the time being):
Jan/1 | jan1.item1.desc
| jan1.item2.desc
| jan1.item3.desc
Jan/2 | jan2.item1.desc
| etc.
So I think I need to know two things: how to construct a correct query (but it could be that this is as simple as Item.where("date > ? < ?", lower_bound, upper_bound)) and how to translate that into the view.
I have also thought about a hash with a key for each individual day and an array for the values, but I'd have to construct that like above(repetition) which I expect is not very elegant.
Using GROUP BY does not seem to get me anything different (apart from the grouping, of course, of the items) to work with than other queries. Just an array of objects, but I might do this wrong.
Sorry if it is a basic question. I am relatively new to the field (and programming in general).
If you're making a calendar, you probably want to GROUP BY date:
SELECT COUNT(*) AS instances, DATE(`date`) AS on_date FROM items GROUP BY DATE(`date`)
This is presuming your column is literally called date, which seeing as how that's a SQL reserved word, is probably a bad idea. You'll need to escape that whenever it's used if that's the case, using ``` here in MySQL notation. Postgres and others use a different approach.
For instances in a range, what you want is probably the BETWEEN operator:
#items = Item.where("`date` BETWEEN ? AND ?", lower_bound, upper_bound)
There's got to be a better way!
I have a bunch of log records stored in a List. Each log record has a CreateDate field and what I'm trying to do is find an efficient way to query the List object to find the day of the week with the most log records (Monday, Tuesday, etc...).
I'm thinking that this can be done using Linq but I don't know Linq well enough. It may be that I'll have to do this the long way by getting a count for each specific day and then comparing them against each other to find the day with the most logs but I'm hoping that someone will be able to show me a cool way to do it.
Thanks.
Loglist.GroupBy(log => log.CreateDate)
.Select(list => new { DayOfWeek = list.Key.DayOfWeek, Count = list.Count()})
.OrderByDescending(list=>list.Count);
This will return a list of (DayOfWeek, Count) in Descending Order. If you need only the largest count dayofweek, apply .First() at the end of the above list.