0

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
user2902180
  • 1,021
  • 3
  • 12
  • 12
  • It should "just work", if `context.Product` is an `IQueryable` managed by your ORM. – ta.speot.is Oct 25 '13 at 10:48
  • @ta.speot.is Only if the database collation of `ProductName` is case insensitive. Apparently, it isn't. – Gert Arnold Oct 25 '13 at 11:54
  • @GertArnold I considered that but thought it more like that OP has done something like creating their own IContext interface with IEnumerable Products. – ta.speot.is Oct 25 '13 at 23:17

5 Answers5

2
public ActionResult AddProduct(MyModel model)
{
var Result = context.Product.Where(s => s.ProductName.ToUpper() ==  model.NewProductName.ToUpper()).FirstOrDefault();
return view();
}
Taj
  • 1,718
  • 1
  • 12
  • 16
0

what about

var Result = context.Product.Where(s => s.ProductName.ToUpper() == model.NewProductName.ToUpper()).FirstOrDefault();
Luke94
  • 712
  • 5
  • 14
0

Use ToLower() on both product names when comparing:

var Result = context.Product
                    .Where(s => s.ProductName.ToLower() == model.NewProductName.ToLower()).FirstOrDefault();
DGibbs
  • 14,316
  • 7
  • 44
  • 83
0

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();
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
0

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.

Community
  • 1
  • 1
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104