将 SQL 转换为 LINQ 格式的 where 子句

Posted

技术标签:

【中文标题】将 SQL 转换为 LINQ 格式的 where 子句【英文标题】:Converting SQL to LINQ formatting where clause 【发布时间】:2012-09-06 10:03:23 【问题描述】:
 Qry = "select childID,ChildEmail,ChildUserID, ChildPassword from ChildProfile ";
 Qry = Qry + " where ucase(ChildFName) = '" 
           + Strings.UCase(Strings.Trim(Txtname.Text)) 
           + "' and ucase(childlname) = '" 
           + Strings.UCase(Strings.Trim(txtSurName.Text)) 
           + "' and childmobile = '" 
           + Strings.Trim(txtMobile.Text) + "'";

我们如何将此查询转换为 LINQ

List<PatientDTO> lstChildProf = objService.GetAllPatient();
    lstChildProf = new List<PatientDTO>(
        from l in lstChildProf where 
        );

这里的where子句不知道怎么写

请帮忙

【问题讨论】:

【参考方案1】:

类似:

List<PatientDTO> lstChildProf = objService.GetAllPatient()
    .Where( l => l.ChildFName == Strings.UCase(Strings.Trim(Txtname.Text))
            && l.ChildName == Strings.UCase(Strings.Trim(txtSurName.Text)) 
            && l.chiuldMob == Strings.Trim(txtMobile.Text))
    .ToList<PatientDTO>();

【讨论】:

错误 14 无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.List”。存在显式转换(您是否缺少演员表?) 要么将List&lt;PatientDTO&gt; 更改为IEnumerable&lt;PatientDTO&gt;,要么将ToList() 放在该代码的末尾。 @vini,我不知道GetAllPatient()方法返回的是什么类型,但是你可以把.ToList&lt;PatientDTO&gt;()放在它的最后作为Arran suggested

以上是关于将 SQL 转换为 LINQ 格式的 where 子句的主要内容,如果未能解决你的问题,请参考以下文章

将包含 where、group by、sum 和必须的 MySQL 语句转换为 Linq to Sql

如何将SQL查询转换为LINQ?

将 SQL 子查询转换为包含 IN、DISTINCT 关键字的 LINQ 格式

使用 SQL Server 数据库将 SQL 查询转换为 Linq

如何将以下 SQL 语句转换为 LINQ 方法语法

如何将 SQL 中的多个内部联接转换为 LINQ?