C# 如何在LINQ中使用字符串进行查询

C# 如何在LINQ中使用字符串进行查询,c#,linq,C#,Linq,我使用的是LINQ,最初是通过表中的主键进行搜索。但是,现在因为我需要通过许多页面传递我的搜索字段(这使得URL非常大)。我想通过改变从在页面之间传递guid到在页面之间传递字符串来解决这个问题。这是我之前的经历: public ActionResult TechKnowledgebaseList(Guid? createdById) { var model = db.Knowledgebases.AsQueryable(); if (createdById != null

我使用的是LINQ,最初是通过表中的主键进行搜索。但是,现在因为我需要通过许多页面传递我的搜索字段(这使得URL非常大)。我想通过改变从在页面之间传递guid到在页面之间传递字符串来解决这个问题。这是我之前的经历:

public ActionResult TechKnowledgebaseList(Guid? createdById)
{
     var model = db.Knowledgebases.AsQueryable();

     if (createdById != null & createdById != Guid.Empty)
     {
           model = model.Where(k => k.CreatedById == createdById);
           ViewBag.CreatedBy = db.Users.Where(c => c.UserId == createdById).First().FullName;
           ViewBag.CreatedById = createdById;
     }
     model = model.OrderBy(k => k.CreatedDate);
     var result = model.ToList();

     return View(result);
}
以下是我现在要做的:

public ActionResult TechKnowledgebaseList(string? createdBy)
{
     var model = db.Knowledgebases.AsQueryable();

     if (createdBy != null)
     {
          model = model.Where(c => c.CreatedBy.FullName == createdBy);
          ViewBag.CreatedBy = db.Users.Where(c => c.UserName == createdBy).First().FullName;
          ViewBag.CreatedBy = createdBy;
     }
     model = model.OrderBy(k => k.CreatedDate);
     var result = model.ToList();

     return View(result);
}
正如你所看到的,我现在正在通过一个刺(可以是空的)。但是,我得到了一个错误:

运算符“==”不能应用于“string”和“string”类型的操作数


您已经发现在字符串和可为空的字符串之间不存在
=
的运算符重载

在任何情况下,可为空的字符串都没有意义,因为普通的旧字符串已经可以为空了。将方法的签名更改为

public ActionResult TechKnowledgebaseList(string createdBy)

而且它会像预期的那样工作,您仍然可以根据需要将
null
或空字符串传递给您的方法。

您已经发现在字符串和可空字符串之间没有
=
的运算符重载

在任何情况下,可为空的字符串都没有意义,因为普通的旧字符串已经可以为空了。将方法的签名更改为

public ActionResult TechKnowledgebaseList(string createdBy)

而且它将按预期工作,您仍然可以根据需要将
null
或空字符串传递给您的方法。

无需将
字符串
包装为
可空
类型,因为默认情况下:

public ActionResult TechKnowledgebaseList(string? createdBy)
只需去掉
,就可以了:

public ActionResult TechKnowledgebaseList(string createdBy)

无需将
字符串
包装为
可空
类型,因为默认情况下:

public ActionResult TechKnowledgebaseList(string? createdBy)
只需去掉
,就可以了:

public ActionResult TechKnowledgebaseList(string createdBy)

我想c.UserId是字符串?检查字符串时,应使用string.IsNullOrEmpty(字符串值)。不要在字符串上使用“==”,请使用

public ActionResult TechKnowledgebaseList(Guid? createdBy)
{
     string crBy = Guid.GetValueOrDefault(/*some default value or nothing*/).ToString();

     var model = db.Knowledgebases.AsQueryable();

     if (!string.IsNullOrEmpty(crBy)))
     {
          model = model.Where(c => c.CreatedBy.FullName == createdBy);
          ViewBag.CreatedBy = db.Users.Where(c => c.UserName.Equals(crBy)).First().FullName;
          ViewBag.CreatedBy = createdBy;
     }
     model = model.OrderBy(k => k.CreatedDate);
     var result = model.ToList();

     return View(result);
}

我想c.UserId是字符串?检查字符串时,应使用string.IsNullOrEmpty(字符串值)。不要在字符串上使用“==”,请使用

public ActionResult TechKnowledgebaseList(Guid? createdBy)
{
     string crBy = Guid.GetValueOrDefault(/*some default value or nothing*/).ToString();

     var model = db.Knowledgebases.AsQueryable();

     if (!string.IsNullOrEmpty(crBy)))
     {
          model = model.Where(c => c.CreatedBy.FullName == createdBy);
          ViewBag.CreatedBy = db.Users.Where(c => c.UserName.Equals(crBy)).First().FullName;
          ViewBag.CreatedBy = createdBy;
     }
     model = model.OrderBy(k => k.CreatedDate);
     var result = model.ToList();

     return View(result);
}

string?
没有意义,因为它是引用类型。只需将
放在一边。
字符串?createdBy
compiles?@Thomas是的,我在那一行没有收到错误。
string?
没有意义,因为它是引用类型。只需将
放在一边。
字符串?createdBy
compiles?@Thomas是的,我在那行没有收到错误。