LINQ results change at the end of the for loop

When I execute a set of LINQ queries to a data source (I use LINQ-to-SQL, but this happens here using only an object List<string>), I end up with a different result at the end of my checks.

In particular, the code below tries to find if a fully functional domain name (FQDN) exists in the list of host names (not all of which will be FQDNs or in the same domain, but it is important to specify the host ID for me). The search tries to find if "host-6.domain.local"either any of its subcomponents (i.e. "host-6.domain"or "host-6") exists in the list, which they don’t have. Inside the for loop, we get the expected results, but as soon as the for loop is completed, I get a result that contains all the contents of the list, which sounds to me as if it is trying to find elements that match an empty string.

void MyMethod()  
{  
    string fqdn = "host-6.domain.local";  
    string[] splitFqdn = fqdn.Split('.');  
    List<string> values = new List<string>();  
    values.add("host-1");  
    values.add("host-2.domain.local");  
    values.add("host-3.domain.local");  
    values.add("host-4");  
    values.add("host-5.other.local");  
    IEnumerable<string> queryResult = null;  
    for (int i = splitFqdn.Length; i > 0; i--)  
    {  
        result =  
            from value in values  
            where value.StartsWith(  
                string.Join(".", splitFqdn.Take(i)))  
            select value;  
        Console.WriteLine(  
            "Inside for loop, take " + i + ": "  + result.Count());  
    }  
    Console.WriteLine();  
    Console.WriteLine(  
        "Outside for loop: " + result.Count());  
}

Why is this happening and how can I get the exact results that I can get after completing the for loop?

+4
source share
3 answers

LINQ.

, ...

result =  
    from value in values  
    where value.StartsWith(  
        string.Join(".", splitFqdn.Take(i)))  
    select value;  

, -, ... , result.count()

, result.count() i, for, , .

, .ToList() , , ... , .

void MyMethod()  
{  
    string fqdn = "host-6.domain.local";  
    string[] splitFqdn = fqdn.Split('.');  
    List<string> values = new List<string>();  
    values.add("host-1");  
    values.add("host-2.domain.local");  
    values.add("host-3.domain.local");  
    values.add("host-4");  
    values.add("host-5.other.local");  
    IEnumerable<string> queryResult = null;
    List<string> correctResult = null;
    for (int i = splitFqdn.Length; i > 0; i--)  
    {  
        queryResult =  
            from value in values  
            where value.StartsWith(  
                string.Join(".", splitFqdn.Take(i)))  
            select value;
        correctResult = queryResult.ToList();
        Console.WriteLine(  
            "Inside for loop, take " + i + ": "  + queryResult.Count());  
    }  
    Console.WriteLine();  
    Console.WriteLine(  
        "Outside for loop queryResult: " + queryResult.Count());  
    Console.WriteLine(  
        "Outside for loop correctResult: " + correctResult.Count());  
}

EDIT: nlips , ... , .

void MyMethod()  
{
    string fqdn = "host-6.domain.local";
    string[] splitFqdn = fqdn.Split('.');
    List<string> values = new List<string>();
    values.Add("host-1");
    values.Add("host-2.domain.local");
    values.Add("host-3.domain.local");
    values.Add("host-4");
    values.Add("host-5.other.local");
    values.Add("host-5.other.local");
    IEnumerable<string> queryResult = null;
    List<string> correctResult = new List<string>();
    for (int i = splitFqdn.Length; i > 0; i--)
    {
        correctResult = correctResult
            .Union(values.Where(
                value => value.StartsWith(string.Join(".", splitFqdn.Take(i)))))
            .ToList();
    }
}
+4

Kevin , .ToList() , ( ), , ( ), , , COUNT ( SQL).

, i, 0 for, , .

void MyMethod()  
{  
    string fqdn = "host-6.domain.local";  
    string[] splitFqdn = fqdn.Split('.');  
    List<string> values = new List<string>();  
    values.add("host-1");  
    values.add("host-2.domain.local");  
    values.add("host-3.domain.local");  
    values.add("host-4");  
    values.add("host-5.other.local");  
    IEnumerable<string> queryResult = null;  
    for (int i = splitFqdn.Length; i > 0; i--)  
    {  
        //taking the line referencing i out of the 
        //query expression prevents referencing i
        //after it is set to 0 outside the for loop
        string temp = string.Join(".", splitFqdn.Take(i));
        //since temp isn't changed anywhere else, it won't
        //get set to an invalid value after the loop exits
        result =  
            from value in values  
            where value.StartsWith(temp)  
            select value;  
        Console.WriteLine(  
            "Inside for loop, take " + i + ": "  + result.Count());  
    }  
    Console.WriteLine();  
    Console.WriteLine(  
        "Outside for loop: " + result.Count());  
}
+2

, ToList :

result =  
            (from value in values  
            where value.StartsWith(  
                string.Join(".", splitFqdn.Take(i)))  
            select value).ToList();
+1

Source: https://habr.com/ru/post/1532718/


All Articles