Kcs ss Kcs ss - 1 year ago 74
ASP.NET (C#) Question

String comparison working on DBSet but not on LIST item in C#

In our C# application we need to compare GUID, In Database it is UPPERCASE but suppose in Code it is in LOWERCASE.

I understand we can handle this by string.Compare or string.Equals but I would like to understand following scenario.

var myInstanceId ='GUID';

Following line do perfect comparison,

var applicationSettings = db.ApplicationSettingsSet
.Where(x => x.myInstanceId == myInstanceId).ToList();

may be due to db.ApplicationSettingsSet is EF DB entity

public DbSet<ApplicationSetting> ApplicationSettingSet { get; set; }

But Somehow following code not working where

applicationSettingCategory.ApplicationSettings = applicationSettings
.Where(x => x.myInstanceId == myInstanceId );

May be due to now comparison is happen on LIST of ApplicationSettings & not on DBSet.

What is actual reason of this behavior?

Answer Source

This one is linq to sql:

var applicationSettings = db.ApplicationSettingsSet.Where(x => x.myInstanceId == myInstanceId).ToList();

translates compare statement to SQL and actual comparison is done in database. Depending if database is case sensitive it can change, so I assume your database is not case sensitive.

Where this is linq to objects:

applicationSettingCategory.ApplicationSettings = applicationSettings.Where(x =>  x.myInstanceId == myInstanceId );

Here you have to specify comparison to be not case sensitive like for String.Equals.