在C#中检查多个值的条件运算符 [英] Conditional operator for more than 1 values checking in C#

查看:106
本文介绍了在C#中检查多个值的条件运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我解决一下如何检查多个值来检查c#lamda表达式



Could you please help me that how to check more than 1 values to check in c# lamda expression

DocumentationLink = context.APIDocumentationTypes.Where(x => x.DocumentationTypeId == a.DocumentationTypeId).Select(x => x.DocumentationDescription).FirstOrDefault().ToLower() != "value1" ? a.DocumentationLink : "",





i需要查看



i need to check with

value1

value2

在同一行。



我尝试了什么:



in same line.

What I have tried:

<pre>DocumentationLink = context.APIDocumentationTypes.Where(x => x.DocumentationTypeId == a.DocumentationTypeId).Select(x => x.DocumentationDescription).FirstOrDefault().ToLower() != "value1" && "value2"? a.DocumentationLink : "",

推荐答案

尝试:

Try:
string result = context.APIDocumentationTypes.Where(x => x.DocumentationTypeId == a.DocumentationTypeId)
                                             .Select(x => x.DocumentationDescription)
                                             .FirstOrDefault().ToLower();
DocumentAtionLink = ( result != "value1" && result != "value2") ? a.DocumentationLink : "";

但请注意如果没有符合选择的元素,则FirstOrDefault 将返回 null - at你的 ToLower 调用将导致 NullReferenceException

But do note that FirstOrDefault will return null if there are no elements that matched your Select - at which point your ToLower call will cause a NullReferenceException.


这是一个< b> 猜测 你可能需要做什么:
Here's a guess at what you might need to do:
private DocumentationLink getadl(AContext ctx, APIDocumentationTypes a)
{
    var adl = ctx.APIDocumentationTypes.Where(x => x.DocumentationTypeId == a.DocumentationTypeId)

        .Select(x => x.DocumentationDescription.ToLower())

        .FirstOrDefault(
            descr => !(descr == "value1" || descr == "value2")
    );

    // what is the default Type here if no match is found ?
    return (adl == "") ? null : a.DocumentationLink;
}

当然,你可以在'选择查询中创建中间变量,如有必要/

Of course, you can create "intermediate" variables in a 'Select query, if necessary/


这篇关于在C#中检查多个值的条件运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆