简体   繁体   中英

How to update a list based on more than one Condition using LINQ C#

I have a String with Pipe Symbol " | " separated, I'm splitting the string and converted into a List<string> . If the List<string> contains a string " fax ", then replace the string into " A " as same as string " phone " to string "B" using inline Single LINQ Statement. Don't try to replace the base string str

string str = "fax|mobile|phone";
str.Split('|').Where(i => i.ToLowerInvariant() == "fax" || i.ToLowerInvariant() == "phone").ToList();

So, my expected output should be

List<string>() {"A", "B"}

Use select to transform your output.

        str.Split('|')
           .Where(i => i.ToLowerInvariant() == "fax" || i.ToLowerInvariant() == "phone")
           .Select(x=> x=="fax"? "A" : x=="phone"? "B" : x)
           .ToList();

It is something like this:

string str = "fax|mobile|phone";
var result = str.Split('|').Select(i => 
    string.Equals(i, "fax", StringComparison.InvariantCultureIgnoreCase) ? "A" : 
    string.Equals(i, "phone", StringComparison.InvariantCultureIgnoreCase) ? "B" : 
    null)
    .Where(i => i != null)
    .ToList();

Please don't change the case of a string to compare it. There are perfectly good methods to do case insensitive comparisons.

This code becomes quite unreadable pretty easily. A better solution is to use a separate Dictionary<,> :

var dict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
dict.Add("fax", "A");
dict.Add("phone", "B");

string str = "fax|mobile|phone";

var result = str.Split('|').Select(i => {
        string r;
        dict.TryGetValue(i, out r);
        return r;
    })
    .Where(i => i != null)
    .ToList();

Try this:

        string str = "fax|mobile|phone";
        var result = str.Split('|').Where(i => i.ToLowerInvariant() == "fax" || i.ToLowerInvariant() == "phone").Select(i =>
            i.ToLowerInvariant() == "fax" ? "A" : "B").ToList();

I'm sure there is a better solution but here is my take:

string str = "fax|mobile|phone";
List<string> list = str.Split('|')
                       .Select(x => x.ToLowerInvariant() == "fax" ? "A" : x.ToLowerInvariant() == "phone" ? "B" : null)
                       .ToList();
list.Remove(null);

The list.Remove(null); could be replaced by a Where clause to get a one liner:

List<string> list = str.Split('|')
                       .Select(x => x.ToLowerInvariant() == "fax" ? "A" : x.ToLowerInvariant() == "phone" ? "B" : null)
                       .Where(x => x != null)
                       .ToList();

A good idea would be to have a separate method to get the matched strings:

public string GetMatch(string s)
{
    // Easier to maintain
    return s.ToLowerInvariant() == "fax" ? "A" : s.ToLowerInvariant() == "phone" ? "B" : null;
}

Then do:

List<string> list = str.Split('|')
                       .Select(x => GetMatch(x))
                       .Where(x => x != null)
                       .ToList();

Base on xanatos solution

var string s = string.Empty;
var dic = new Dictionary<string,string>(StringComparer.InvariantCultureIgnoreCase)
{
  {"fax","a"},{"phone","b"}
}
var result = (from w in str.split('|') where dic.TryGetValue(w, out s) select s).toList();

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