简体   繁体   中英

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.

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);

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.

 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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM