0

why part 2 is returning null.

the strange part is that if i search by string type then null returns, but if i search by integer type or bool type. then i get brand object.

Part 1

//....     OK code
                 public static Brand GetBrand(string name)
                {
                    DataContext db = new DataContext();
                    using (db)
                    {
                        Brand b = (from v in db.Brands
                                   where v.Name == name
                                   select v).FirstOrDefault();
                        return b;
                    }
                }




               [HttpGet]
               public IHttpActionResult Get(string name)
               {
                   Brand b = MobileHandler.GetBrand(name);
                   return Ok(b);
               }

Part 2

//.....   Return null
       public static List<Brand> GetBrands()
            {
                DataContext db = new DataContext();
                using (db)
                {
                    return db.Brands.ToList();
                }
            }

           [HttpGet]
           public IHttpActionResult Get(string name)
           {
               return Ok((from v in MobileHandler.GetBrands()
                          where v.Name == name
                          select v).FirstOrDefault());
           }

But I get the following error:

<Brand xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DataLayer.MobileMgt" i:nil="true"/>
Naveed Ullah
  • 541
  • 4
  • 10
  • 1
    Depends what `MobileHandler.GetBrand` is doing. You're obviously searching for a brand which doesn't exist. In fact, your code explicitly expects no matches to be found, since it uses `FirstOrDefault()` rather than `First()` – Rob Apr 21 '17 at 05:57
  • show implementation of GetBrand & GetBrands and make sure brand you are searching exists. – Mukesh Modhvadiya Apr 21 '17 at 06:03
  • Yes i have confirmed that brand exists – Naveed Ullah Apr 21 '17 at 06:19
  • what is type for V.Name?? , As V.Name == name will compare type and value both, try using V.Name.Equals(name) if you care about only value. – Dirty Developer Apr 21 '17 at 06:23
  • still returns null. and type is string – Naveed Ullah Apr 21 '17 at 06:32
  • the strange part is that only with string type i am having this issue, not with other types like int and bool – Naveed Ullah Apr 21 '17 at 06:33
  • First() returns error. "Sequence contains no elements" although i am getting brand name through first portion of my code. #Rob – Naveed Ullah Apr 21 '17 at 06:38
  • Can you give us what string are you using? There might be some special characters that are compared differently in SQL and in .NET . Did you try different strings? – Euphoric Apr 21 '17 at 06:41
  • No code part returns the brand name. There is only code returning a brand by name. What are you talking about? – Sir Rufo Apr 21 '17 at 06:42
  • http://localhost:28906/services/brand?name=apple – Naveed Ullah Apr 21 '17 at 06:42
  • Be careful about upper/lower case comparisons. Make sure your character cases are same or use case-insensitive comparison. – Euphoric Apr 21 '17 at 06:43
  • #Sir Rufo "I know no code is returning brand name. i am returning Brand object. the problem is when i search by brand name. then it returns null but if i search by integer Id then i get Brand object" – Naveed Ullah Apr 21 '17 at 06:49

1 Answers1

1

I believe this is issue of case-sensitive comparison. This is indicated by your code working for ints and bools and not strings and by a fact that brand name is "Apple" not "apple"

Entity Framework uses case comparison settings on the SQL server, which is usually set to be case-insensitive. In your case then "Apple" is equals to "apple". This is the first case.

But .NET uses case-sensitive comparison, which is the second case. So "apple" wont be equal to "Apple". To fix this instead of v.Name == name use v.Name.Equals(name, StringComparison.OrdinalIgnoreCase).

Community
  • 1
  • 1
Euphoric
  • 12,645
  • 1
  • 30
  • 44