I am using LINQ and I was Originally searching by the Primary Key in the table. However, now since I need to pass the my search fields through numerous pages (which makes the URL very large). I want to fix this by changing from passing Guids between pages to passing the Strings between pages. This is what I had prior:
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);
}
Here is what I am trying to do now:
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);
}
As you can see I am passing in a sting now (that can be empty). However, I am getting the error:
Operator '==' cannot be applied to operands of type 'string' and 'string?'
string?doesn't make sense because it is a reference type. Just leave the?away.string? createdBycompiles?