开发者

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-0开发者_StackOverflow社区2C72679334A')

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜