10

I have a table with a following format.

PID     ID       Label        Value
------------------------------------------
1       1        First Name    Jenna
1       2        DOB           10/12/1980

I need to retrieve all PIDs where First name starting with J and Month of DOB is 10.

in my code, I retrieve these in DataTable in C# and then tried to use LINQ to retrieve the results I want. This is just an example. These Labels could be anything user defines.

using LINQ I am able to retrieve all PIDs where First Name start with J, but every time I tried to Cast Value for DOB I get cast not valid error. I cannot change the column type in the database since Value could contain any type of information.

Here's a piece of my code. I am new to LINQ, and still trying to figure out around it.

var resultQuery = from r in query.AsEnumerable()
where (r.Field<string>("Label") == Label  &&
r.Field<DateTime>("Value").Month == 10)
select r.Field<int>("PID");
1
  • What's the DateTime value of "Jenna"? I'm not sure that this is feasible.
    – Bobson
    Oct 19, 2012 at 18:30

2 Answers 2

4

Since not all items in the Value column of the table are convertible to DateTime, what you have will fail on invalid conversions. You can add in a clause that first checks that the value is a DateTime and only if it is, converts it and checks the .Month property.

DateTime d;
var resultQuery = from r in query.AsEnumerable()
                  where (r.Field<string>("Label") == Label &&
                      DateTime.TryParse(r.Field<string>("Value"), out d) && 
                      d.Month == 10)
                  select r.Field<int>("PID");

To potentially improve readability, you could also extract this out into a separate method:

var resultQuery = from r in query.AsEnumerable()
                  let d = TryGetDate(r.Field<string>("Value"))
                  where (r.Field<string>("Label") == Label &&
                      d != null && 
                      d.Month == 10)
                  select r.Field<int>("PID");

private DateTime? TryGetDate(string value)
{
    DateTime d;
    return DateTime.TryParse(value, out d) ? d : default(DateTime?);
}
10
  • 3
    For the sake of readability and "correctness", I believe it would be better to put the date logic in a separate method (DateTime? TryGetDate(string value)) and return null if not parse-able...
    – ioctlLR
    Oct 19, 2012 at 18:24
  • Thank you both. If I just run the query the way goric suggested on it's own it doesn't error out, but if I am trying to convert resultquery to List and it fails. If I take out <DataTime>.Month == 10, my List works fine. PID = resultQuery.ToList(); Where PID is defined at type of List<int> Oct 19, 2012 at 18:28
  • Not only that, but your code, as is won't work. Even if you can parse the string to a date, you can't just cast it to a date, which is what you are doing here.
    – Servy
    Oct 19, 2012 at 18:28
  • @ioctlLR - That'll fail if it tries to parse that function on the server, though.
    – Bobson
    Oct 19, 2012 at 18:28
  • @Servy: Not sure I follow, TryParse would return true and initialize d, making it ready for use, or would return false and abort the && logic.. What am I missing?
    – goric
    Oct 19, 2012 at 18:30
2

You are going to end up filtering in memory which isn't very efficient.

So first select your data

var data= from r in query.AsEnumerable();

Then filter on the data

var filtered = from item in data
               where item.Label == "Label"
               && Convert.ToDateTime(item.DOB).Month == 10
               select item.PID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.