简体   繁体   中英

Contains returning false

i have code like this

foreach (string item in summaryList)
{
    //kelola data
    if(item.Contains("Central"))
    {
        targetSummary = System.Math.Round(_context.Target.Where(x => x.Month == monthId && x.Region.Contains("Central") && x.Type == "SellThru").Select(y => y.Target1 ?? 0).Sum(), 2);
    }
    if (item.Contains("East"))
    {
        targetSummary = System.Math.Round(_context.Target.Where(x => x.Month == monthId && x.Region.Contains("East") && x.Type == "SellThru").Select(y => y.Target1 ?? 0).Sum(), 2);
    }
    if (item.Contains("West"))
    {
        targetSummary = System.Math.Round(_context.Target.Where(x => x.Month == monthId && x.Region.Contains("West") && x.Type == "SellThru").Select(y => y.Target1 ?? 0).Sum(), 2);
    }
    else
    {
        targetSummary = System.Math.Round(_context.Target.Where(x => x.Month == monthId && x.Region.Contains(item) && x.Type == "SellThru").Select(y => y.Target1 ?? 0).Sum(), 2);
    }
}

and the summaryList data is

"ID-TR-Central, ID-TR-Java, ID-TR-East, ID-TR-West"

i dont know why

item.Contains("Central") and item.Contains("East")

is returning false that makes targetSummary value is infinity, but

item.Contains("West")

is doing fine

i just dont know what is my mistakes. sorry about my english, i hope you understand and i appreciate your help. thanks

First thing: Post your code for populating summaryList. I assume you meant it's actually {"ID-TR-Central", "ID-TR-Java", "ID-TR-East, "ID-TR-West"} , not "ID-TR-Central, ID-TR-Java, ID-TR-East, ID-TR-West" .

If you really are feeding a single string, then every criteria is going to be matched and targetSummary will be overwritten.

If not, you still have a problem because your last if statement contains an else branch. You can see more clearly what would happen if your string doesn't contain "West" with reformatting:

foreach (string item in summaryList)
{
    //kelola data
    if(item.Contains("Central"))
    {
        targetSummary = System.Math.Round(_context.Target.Where(x => x.Month == monthId && x.Region.Contains("Central") && x.Type == "SellThru").Select(y => y.Target1 ?? 0).Sum(), 2);
    }

    if (item.Contains("East"))
    {
        targetSummary = System.Math.Round(_context.Target.Where(x => x.Month == monthId && x.Region.Contains("East") && x.Type == "SellThru").Select(y => y.Target1 ?? 0).Sum(), 2);
    }

    if (item.Contains("West"))
    {
        targetSummary = System.Math.Round(_context.Target.Where(x => x.Month == monthId && x.Region.Contains("West") && x.Type == "SellThru").Select(y => y.Target1 ?? 0).Sum(), 2);
    }
    else
    {
        targetSummary = System.Math.Round(_context.Target.Where(x => x.Month == monthId && x.Region.Contains(item) && x.Type == "SellThru").Select(y => y.Target1 ?? 0).Sum(), 2);
    }

The first two conditions don't matter because your final condition forces anything without "West" into the final else branch. targetSummary will be overwritten regardless of previous matching conditions.

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