[英]How to apply the else if condition into the LINQ query.?
I have a list of subjects 'subjectList' I want to get the value based on two conditions like if subjectValue,=null then select subjectValue else if the subject is isDefault=true then select subjectDefaultCode.我有一个主题“subjectList”的列表,我想根据两个条件获取值,例如如果 subjectValue,=null 然后 select subjectValue 否则如果主题是 isDefault=true 然后 select subjectDefaultCode。 I have written an individual LINQ query for both condition that but I am not getting how to apply both the condition in a single query?.我已经为这两个条件编写了一个单独的 LINQ 查询,但我没有得到如何在单个查询中应用这两个条件? Have a look.看一看。
string subjectValueDropDown = string.Empty;
First condition.第一个条件。
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.SubjectValue.= null).Select(k => k.SubjectValue);ToArray());
Second condition.第二个条件。
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.IsDefault == true).Select(k => k.subjectDefaultCode).ToArray());
Possbilities:可能性:
Subject1 SubjectValue=null , isDefault=false
Subject2 SubjectValue=Maths , isDefault=false
Subject3 SubjectValue=null , isDefault=true
Subject4 SubjectValue=null , isDefault=false
In this situation or another situation where any of the subject have SubjectValue.=null then I don't want the isDefault condition to get executed.在这种情况或其他任何主题具有 SubjectValue.=null 的情况下,我不希望执行 isDefault 条件。
Thanks in advance.提前致谢。
Updated: Output should be same as op of below query:更新: Output 应与以下查询的操作相同:
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.SubjectValue != null).Select(k => k.SubjectValue).ToArray());
if(subjectValueDropDown==""){
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.IsDefault == true).Select(k => k.subjectDefaultCode).ToArray());
}
Is it possible to merge both the query into a single query to get the expected output.是否可以将两个查询合并到一个查询中以获得预期的 output。
Try this, it was tested in Visual Studio and values are the same as in your update:试试这个,它在 Visual Studio 中测试过,值与更新中的值相同:
subjectValueDropDown = string.Join(",",
subjectList
.Where(x =>
(x.SubjectValue != null)
|| (x.SubjectValue == null && x.IsDefault)
).Select(k =>
k.SubjectValue != null? k.SubjectValue : k.SubjectDefaultCode
)
.OfType<string>()
.ToArray());
using System.Collections.Generic;
using System.Linq;
namespace Game
{
class Subject
{
public string SubjectValue { get; set; }
public bool IsDefault { get; set; }
public string subjectDefaultCode = "Defauult code.";
}
class Program
{
static void Main(string[] args)
{
var subjectList = new List<Subject>
{
new Subject
{
IsDefault = false,
},
new Subject
{
SubjectValue = "Maths",
IsDefault = false,
},
new Subject
{
IsDefault = false,
},
new Subject
{
IsDefault = false,
},
};
var ubjectValueDropDown = string.Join(",", subjectList.Where(x => x.SubjectValue is not null || x.IsDefault)
.Select(x =>
{
if (x.SubjectValue is not null)
return x.SubjectValue;
if (x.IsDefault)
return x.subjectDefaultCode;
return "";
}));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.