简体   繁体   中英

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. 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?. 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.

Thanks in advance.

Updated: Output should be same as op of below query:

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.

Try this, it was tested in Visual Studio and values are the same as in your update:

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 "";
                }));     
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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