简体   繁体   English

LinQ查询条件

[英]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. 我想检查a.Table1.Id值,如果它等于null,那么我仍然想要该行但忽略where条件。

Any suggestion for using the ternary operator in Linq query or any other approaches for my task. 任何在Linq查询中使用三元运算符或任何其他方法来执行任务的建议。

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: 这是假设我的VB是正确的...在C#我只是写:

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. 我想念的是Skeet先生发现的事情。 It's VB. 这是VB。 Well, I'll let this stick around as a C# sample. 好吧,我会把它作为C#样本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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