0

Here string productname2="xyz" but actually in database PRODUCTNAME is "XYZ" when I compare it return false,so I could not get productcode,here I want not only compare but also some value from this satetement.

orderdetail.PRODUCTCODE = context.PRODUCTs.Where(pro => pro.PRODUCTNAME==productname2)
                         .Select(code => code.PRODUCTCODE).DefaultIfEmpty().Max();

Any help will be appreciated

anis programmer
  • 989
  • 1
  • 10
  • 25
  • 2
    How about using ToLowerInvariant() on both sides of your comparison? – wkl Jan 09 '17 at 09:00
  • 1
    @wkl `ToLower` has internationalisation issues - always better to use the relevant override of `string.Equals(...)` – Rowland Shaw Jan 09 '17 at 09:02
  • 5
    I don't believe that this is best solved in the query. Given that this is coming from the DB, it looks like you might better benefit from setting the correct (case-insensitive) collation on the column in the database. By converting case, you are discarding the benefit of any indexes you might have on the column. – spender Jan 09 '17 at 09:04
  • @RowlandShaw You are right, I edited my comment to using ToLowerInvariant. I think that should do it. – wkl Jan 09 '17 at 09:04
  • 1
    @wkl IIRC, that will blow up when it gets translated to SQL. – spender Jan 09 '17 at 09:05
  • could you please specify the data provider you are querying against? objects, sql, ...? –  Jan 09 '17 at 09:08
  • @Jehof Yah,I use entity frame work with Oracle – anis programmer Jan 09 '17 at 09:40
  • @ Andreas Niedermair it is Oracle – anis programmer Jan 09 '17 at 09:40
  • So, it's [tag:linq-to-entities]? –  Jan 09 '17 at 12:44

3 Answers3

3

You have to compare strings ignoring case :

orderdetail.PRODUCTCODE = context.PRODUCTs.Where(pro => pro.PRODUCTNAME.Equals(productname2, StringComparison.InvariantCultureIgnoreCase).Select(code => code.PRODUCTCODE).DefaultIfEmpty().Max();
PMerlet
  • 2,568
  • 4
  • 23
  • 39
  • I strongly suggest to use `string.Equals` instead of `instance.Equals`, as the former call is null-safe :) Additionally, I would also question if you need culture-aware comparison, or ordinal - the later one has better performance ... As always, depending on the data provider you querying against (some calls might not get translated to pure sql ...) –  Jan 09 '17 at 09:04
  • 4
    Will this translate to SQL? – spender Jan 09 '17 at 09:06
  • .Select(code => code.PRODUCTCODE).DefaultIfEmpty().Max(); do not work properly this option,it show syntax error – anis programmer Jan 09 '17 at 09:14
0

Hope this helps

.Where(pro => pro.PRODUCTNAME.Equals(productname2, StringComparison.OrdinalIgnoreCase))

OrdinalIgnoreCase and CultureIgnoreCase just differs in the rules applied for comparison, where the former performs better with less rules than latter.

abc
  • 2,285
  • 5
  • 29
  • 64
0

It works

orderdetail.PRODUCTCODE = context.PRODUCTs.Where(pro => pro.PRODUCTNAME.ToLower().Contains(productname2.ToLower())).Select(code => code.PRODUCTCODE).DefaultIfEmpty().Max();

from this link LINQ Contains Case Insensitive

Community
  • 1
  • 1
anis programmer
  • 989
  • 1
  • 10
  • 25