LINQ Min(dates) - linq

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.

Related

Can't get WORKDAY function to work with a COUNTIF

Trying to work on a system at work that will tell how many error codes were registered by a particular machine on the previous workday. This spreadsheet will need to be able to select only the errors generated on the previous date as this will become a rolling list of data generated across a wide time span. Currently working with the formula
=TODAY(),-1,B2:B17)
where the last array is some shutdown days I've put in to generate a global variable "Yesterday" and trying to use the formula
=COUNTIF(Table1[DateOnly],"="&Yesterday)
to gather the number of records that occurred yesterday.
Can anyone tell me where I'm going wrong?
Found that the issue was when I tried to convert the timestamp in mm/dd/yy hh:mm:ss format to mm/dd/yy and didn't realize that the other information was still hiding in there and confusing the formula. One of my coworkers recommended the use of a ROUNDDOWN function in the use of =ROUNDDOWN(argument,0) to get rid of the time information and just leave me with date.

Inconsistent query results

I have a query that builds a cursor to create invoices. Part of it is the expression "IIF(cuPR.curren="EUR",NULL,rate) AS Taux".
My problem is that my query works fine for January through April and for June, but not for May. I checked the query to identify the problem, I checked and rechecked my data, everything looks fine. The data being the only thing that changes, what else should I check, please??
Usually if there exists a possibility that I will have data that may throw things off like you have here I will use a cast to ensure my field is what I expect.
Something like...
SELECT CAST(CAST(IIF(cuPR.curren="EUR", NULL, rate) AS Numeric(10,5)) AS Taux ...
Upon further research I noticed that in May the first record in the cursor cuPR had "EUR" as "curren". I tried sorting my cursor by "curren DESC", making sure EUR would not be in the first record (USD and GBP are other possible values) and my query went through.
DRapp had given the explanation in response to a previous question of mine:
"Bernard (and others new to VFP). VFP queries actually run the query twice, once for the first record just to confirm the final column types and sizes, then for the actual query of ALL records." In my case one of the columns was NULL...

OBIEE: Casting String to date then date to string

FILTER("source"."recordCount" USING "source"."snapshot_date" =
EVALUATE('TO_CHAR(%1, ''YYYYMMDD'')', TIMESTAMPADD(SQL_TSI_DAY, -7, EVALUATE('TO_DATE(%1, %2)', "source"."snapshot_date" , 'YYYYMMDD'))))
So i have this piece of code here. I know some will say "Just use the AGO function" But somehow it's causing problems because of it's connection with other tables so what I'm trying to achieve here is like a remake. The process goes this way:
The snapshot_date there is actually in varchar format and not date. So it's like "20131016" and I'm trying to change it to a date then subtract 7 days from it using the TIMESTAMPADD function and then finally returning it back to varchar to use it with FILTER.
This snippet somehow works when testing the FILTER using hardcoded values like "20131016" for example but when tested out with the code above all the row are blank. On paper, the process i assumed would happen goes lke this. "20131016" turns to a date with a format of 20131016 (yyyymmdd) and then less 7 days: 20131009 and then turned into char again "20131009" to be used in the filter.
But somehow that doesn't happen. I think the data format is not applying either to the string->date or the date->string conversion. which results to the values not getting a match at all.
Anyone have any idea what's wrong with my code?
By the way I've already tried to use CAST instead of EVALUATE or TO_TIMEDATE with the same result. Oh and this goes to the formula of the column in BMM.
Thanks
You might get some clues by looking at the SQL generated by the BI Server. I can't see any issues with your column expression, so I wouldn't limit your debugging to that alone.
A query returning nulls is often caused by incorrect levels being set (especially on logical table sources, but potentially on a measure column too). This will often result in some form of SELECT NULL FROM ... in the physical SQL.
Try this :
FILTER("source"."recordCount" USING "source"."snapshot_date" =
EVALUATE('TO_CHAR(%1, %2)', TIMESTAMPADD(SQL_TSI_DAY, -7, EVALUATE('TO_DATE(%1, %2)', TO_CHAR("source"."snapshot_date" , 'YYYYMMDD') , 'YYYYMMDD')) , 'YYYYMMDD'))

Linq Lambda sort on DateField

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.

LINQ group by question

I started playing around with Linq today and ran into a problem I couldn't find an answer to. I was querying a simple SQL Server database that had some employee records. One of the fields is the full name (cn). I thought it would be interesting to group by the first name by splitting the full name at the first space. I tried
group by person.cn.Split(separators)[0]
but ran into a lengthy runtime exception (looked a lot like a C++ template instantiation error).
Then I tried grouping by a few letters of the first name:
group by person.cn.Substring(0,5)
and that worked fine but is not what I want.
I'm wondering about two things:
Why does the first example not work when it looks so close to the second?
Knowing that behind the scenes it's SQL stuff going on, what's a good way to do this kind of thing efficiently
Thanks,
Andrew
Split has no translation into SQL.
So, how to do this string manipulation without split? Cheat like hell (untested):
string oneSpace = " ";
string fiftySpace = " ";
var query =
from person in db.Persons
let lastname = person.cn.Replace(oneSpace, fiftySpace).SubString(0, 50).Trim()
group person by lastname into g
select new { Key = g.Key, Count = g.Count };
The reason your first attempt didn't work is because LINQ to SQL uses Expression Trees to translate your query into SQL. As a result any code that isn't directly translatable into SQL is an exception - this includes the call to Split.
Thanks guys, I'll try the "Replace" trick to see if that runs. I'm very intrigued by LINQ but now it looks like there's some hidden mysteriousness where you have to know what your LINQ queries translate into before being able to use it effectively.
The core problem is of course that I don't know SQL very well so that's where I'll start.
Edit:
I finally tried the "Replace" today and it works. I even got to sort the grouped results by count so now I have a pareto of name in my company. It's horrendously slow, though. Much faster to select everything and do the bucketing in C# directly.
Thanks again,
Andrew

Resources