Hari Gillala
Hari Gillala

Reputation: 11926

LinQ Query Where Condition

I am trying to import data into a new database, but some of the columns in the old database are null.

In one of my methods, I am using the query below to get the records, like I said already there is a column in the rows which has a null value for some records.

Guid nameGuid= new guid('CCECE54B-EE14-4463-8A0B-02C72679334A')

MySubQuery = from a in MainQuery
              where a.Table1.Id.Equals(nameGuid)
        Select a;

I want to check for a.Table1.Id value, if it is equal to null, then I still want the row but ignore the where condition.

Any suggestion for using the ternary operator in Linq query or any other approaches for my task.

Upvotes: 3

Views: 62390

Answers (3)

Jayesh D. Shelar
Jayesh D. Shelar

Reputation: 21

 var datarows = (from row in dt.AsEnumerable()
                            where row.Field<Guid>("Id") == new 
                 Guid(ddlCode.SelectedValue.ToString())
                            select row);

After that

     DataTable newDt=datarows.CopyToDataTable();
     var sampleName=newDt.Rows[0]["SampleName"].ToString(); 

Upvotes: 0

Mikael &#214;stberg
Mikael &#214;stberg

Reputation: 17176

How about:

MySubQuery = from a in MainQuery
             where a.Table1.Id == null || a.Table1.Id.Equals(nameGuid)
             select a;

Or am I missing something?

EDIT:

The thing I am missing is what Mr Skeet spotted. It's VB. Well, I'll let this stick around as a C# sample.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503839

Sounds like you want:

MySubQuery = from a in MainQuery
             where a.TableId.Id Is Nothing OrElse a.Table1.Id.Equals(nameGuid)

That's assuming my VB is correct... in C# I'd just write:

var query = from a in mainQuery
            where a.TableId.Id == null || a.TableId.Id == nameGuid
            select a;

or using the extension method directly:

var query = mainQuery.Where(a => a.TableId.Id == null || 
                                 a.TableId.Id == nameGuid);

Upvotes: 10

Related Questions