Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8722039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:19:23+00:00 2026-06-13T07:19:23+00:00

I have a table with a following format. PID ID Label Value —————————————— 1

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:19:24+00:00Added an answer on June 13, 2026 at 7:19 am

    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?);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table named People in the following format: Date | Name .
After doing some joins and stuff I have table in the following format name
I have a simple SQL table of the following format: date | value ============
I have table with following structure and records. table t1 +------+-------------+---------------------+ | name |
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
I have the following database structure: Sites table id | name | other_fields Backups
I have sql server 2008 db table FILE_DETAILS in following format. ID FileName Filesize_in_MB
I have a php query the returns the following JSON format from a table.
I have a php query the returns the following JSON format from a table.
I have a table in the following format Country_Code | 1960 | 1961 |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.