简体   繁体   English

如何在LINQ中解引用可能为null的字符串

[英]How to dereference a string that might be null in LINQ

Let's say I need to compare an object to other objects stored in a table called Indexes in my DB. 假设我需要将一个对象与数据库中名为“索引”的表中存储的其他对象进行比较。 I need to compare by the object's X property, which is a string, but it might be null . 我需要按对象的X属性进行比较,该属性是一个字符串,但它可能为null

I also have to trim my comparedObject's X property before comparing. 在比较之前,我还必须修剪我的compareObject的X属性。 I tried to do the following: 我尝试执行以下操作:

List<Guid> Ids = DataContext.Indexes.Where(ci =>
                 (comparedObject.X != null && ci.X != null ? 
                 ci.X == comparedObject.X.Trim() :
                 (ci.X == null || ci.X == string.Empty) && (comparedObject.X == null || comparedObject.X == string.Empty))).Select(ci => ci.Id).ToList();

And even though comparedObjects.X is null it still throws a null reference exception for the comparedObject.X.Trim() expression. 而且即使comparedObjects.Xnull它仍然抛出了一个空引用异常comparedObject.X.Trim()表达。

I assume that happens due to the linq conversion? 我认为这是由于linq转换而发生的?

Is there a prettier way to trim the X property without having to assign comparedObject.X an empty string in case it's null before the query ? 有没有修剪一个漂亮的方式X性能,而无需指定comparedObject.X一个空字符串的情况下,它的查询之前是空?

EDIT: I'd like to elaborate - this query was reduced for simplicity here, I am also comparing about 6 other properties. 编辑:我想详细说明-为简化起见,此查询已减少,我还比较了其他6个属性。 I'd like to keep this in 1 query and not separate to 2 queries that differ in the X property alone. 我想将其保留在1个查询中,而不要单独包含在X属性中的2个查询中。 Trimming outside the query is my current solution, I was hoping for a in-statement solution in case there is any :) 在查询之外进行修剪是我当前的解决方案,我希望有一个陈述中的解决方案,以防万一:)

Thanks! 谢谢!

And even though comparedObjects.X is null it still throws a null reference exception for the comparedObject.X.Trim() expression. 而且,即使compareObjects.X为null,它仍然为compareedObject.X.Trim()表达式引发null引用异常。

you better do a null check before the linq statement 你最好在linq语句之前做一个空检查

if(comparedObject !=null && !string.IsNullorEmpty(comparedObject.X))
{
    // your code goes here 
}

below code 下面的代码

(ci.X == null || ci.X == string.Empty) && (comparedObject.X == null || comparedObject.X == string.Empty)

can change to 可以更改为

string.IsNullorEmpty(ci.X) && string.IsNullorEmpty(comparedObject.X)

And i would change code as below 我将如下更改代码

List<Guid> Ids = DataContext.Indexes.Where(ci =>
                 (string.IsNullorEmpty(ci.X) && string.IsNullorEmpty(comparedObject.X)) || ci.X == comparedObject.X.Trim())
                .Select(ci => ci.Id).ToList();

may be you can try: 也许您可以尝试:

List<Guid> Ids = DataContext.Indexes.Where(ci =>ci.X != null ? ci.X == comparedObject.X==null?"":comparedObject.X.Trim() :
                                 (comparedObject.X == null || comparedObject.X == null)))
                            .Select(ci => ci.Id).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM