Reputation: 1041
I have Product on database. Product name is "WINNER".
İf i add new product with name "Winner" , result is null.
How can i match WINNER and Winner as same ?
public ActionResult AddProduct(MyModel model)
{
var Result = context.Product.Where(s => s.ProductName == model.NewProductName).FirstOrDefault();
return view();
}
Any help will be greatly appreciated.
Upvotes: 0
Views: 114
Reputation: 46551
You could use Equals
with a StringComparison
type:
var Result = context.Product.FirstOrDefault(s => s.ProductName.Equals(model.NewProductName, StringComparison.InvariantCultureIgnoreCase));
I also merged the Where()
with FirstOrDefault()
.
See this post for more information about string comparisons.
Upvotes: 0
Reputation: 78535
Unfortunately there is no method for matching case-insensitive strings in Linq-to-SQL, so you need to convert both strings to the same casing:
var Result = context.Product.Where(s =>
// Make sure both sides are non-null for comparison
s.ProductName != null && model.NewProductName != null &&
// Convert both strings to same case
s.ProductName.ToLower() == model.NewProductName.ToLower()).FirstOrDefault();
Upvotes: 0
Reputation: 14618
Use ToLower() on both product names when comparing:
var Result = context.Product
.Where(s => s.ProductName.ToLower() == model.NewProductName.ToLower()).FirstOrDefault();
Upvotes: 0
Reputation: 712
what about
var Result = context.Product.Where(s => s.ProductName.ToUpper() == model.NewProductName.ToUpper()).FirstOrDefault();
Upvotes: 0
Reputation: 1728
public ActionResult AddProduct(MyModel model)
{
var Result = context.Product.Where(s => s.ProductName.ToUpper() == model.NewProductName.ToUpper()).FirstOrDefault();
return view();
}
Upvotes: 2