[英]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. 在我们的C#应用程序中,我们需要比较GUID,在数据库中为UPPERCASE,但在代码中假定为LOWERCASE。
I understand we can handle this by string.Compare or string.Equals but I would like to understand following scenario. 我了解我们可以通过string.Compare或string.Equals处理此问题,但我想了解以下情况。
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 可能是由于db.ApplicationSettingsSet是EF DB实体
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. 可能是由于现在比较发生在ApplicationSettings的LIST而不是DBSet上。
What is actual reason of this behavior? 此行为的真正原因是什么?
This one is linq to sql: 这是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. 将compare语句转换为SQL,然后在数据库中进行实际比较。 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
. 在这里,您必须指定比较,使其不区分大小写,例如
String.Equals
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.