none
Issue with .Find function RRS feed

  • Question

  • Hello, 

    Having an issue with using the .Find function in a .Net 4.5 Console application.  The problem is that sometimes the .Find works and other times it does not.  I need to process about 100,000 rows based on 55 rules, so I am loading these rules into a List<T> so that I do not have to hit the database for each of the 100,000 rows. 

    So at the start I load up my list and then dump it into a CSV for debugging purposes.  So I know the values are in my List<>, however as I said, some of the time the Find is not working and this is using identical values. 

    I was wondering if there is any need to reset the list back to the beginning so that .Find works every time. 

      internal class RuleEntity : Entity 
        {
            internal string BackupTool { get; set; }
            internal string ServerName { get; set; }
            internal string InstanceName { get; set; }
            internal int KeepDaysFull { get; set; }
            internal int KeepDaysDiff { get; set; }
            internal int KeepDaysLog { get; set; }
            internal string BusinessOwner { get; set; }
            internal string OwnerEmail { get; set; }
            internal bool IsEmailingActive { get; set; }
        }

    So after loading, my list<> lists looks like this (in Excel)

     var re = rulesList.Find(rule => rule.ServerName.ToUpper().Trim() == blob.Container.ToUpper().Trim()
                        && rule.InstanceName.ToUpper().Trim() == blob.Parts.InstanceName.ToUpper().Trim());
    
                    if (re != null)
                    {
                        if (!string.IsNullOrWhiteSpace(re.ServerName) && !string.IsNullOrWhiteSpace(re.InstanceName))
                        {
                            blob.Rule = Utils.GetRuleParts(re.ServerName, re.InstanceName, ref re);
                            bl.WriteDetails(blob, start);
                        }
                    }
                    else
                    {
    
    
                        throw new NotImplementedException();
                    }

    and yet the object re is occasionally null for Server="Server1" and "Instance="dev2016". 

    I know this works if I hit the database every time as that was how the code worked originally.   

    Any ideas? 

    Thanks in advance. 

     

    Saturday, May 12, 2018 3:20 PM

Answers

  • "I was wondering if there is any need to reset the list back to the beginning so that .Find works every time. "

    No you don't need to do this. Find simply enumerates the list and runs the predicate on each element. There is pretty much no way it would randomly fail unless you are changing your list or your condition is wrong. Note that in a LINQ world we would tend to use FirstOrDefault.  

    You said it happens periodically so that would mean if you ran the code, without change 10 times, you'd see a failure at least once?

    What is rulesList? It looks like the list of your rules but it would be what the find action is running against. Presumably it should be List<RulesEntity>. I notice you're using a combination of ToUpper and trim calls. Do you have null or empty values? Are you dealing with foreign characters at all? I would probably recommend using String.Compare instead, although you'd still need to trim if that is an issue.

    Set a breakpoint on your else block and run your code to replicate the issue. Once it fires you will have the exact row that is causing the problem. Using the Immediate window run the rule again to see what you get. My gut instinct, assuming you're enumerating RuleEntity objects, is that your blob variable isn't set properly causing you to get errors. You didn't post the higher level enumeration code so it is hard to say.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, May 14, 2018 2:38 PM
    Moderator

All replies

  • Read what you wrote and assume you don't know what ""


    Sam Hobbs
    SimpleSamples.Info

    Sunday, May 13, 2018 7:25 PM
  • Read what you wrote and assume you don't know what ""

    What?

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, May 14, 2018 5:02 AM
    Moderator
  • Hi The real Slartibartfast,

    >>So at the start I load up my list and then dump it into a CSV for debugging purposes.  So I know the values are in my List<>, however as I said, some of the time the Find is not working and this is using identical values. 

    Why find method is not working, do you clean the List<> and change the List<> object?

    >>I was wondering if there is any need to reset the list back to the beginning so that .Find works every time. 

    If you change the List<> after you use Find method. I would suggest that you assign the list to another empty list as original source. if you want to use find method again, assign it back. 

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 14, 2018 8:19 AM
    Moderator
  • "I was wondering if there is any need to reset the list back to the beginning so that .Find works every time. "

    No you don't need to do this. Find simply enumerates the list and runs the predicate on each element. There is pretty much no way it would randomly fail unless you are changing your list or your condition is wrong. Note that in a LINQ world we would tend to use FirstOrDefault.  

    You said it happens periodically so that would mean if you ran the code, without change 10 times, you'd see a failure at least once?

    What is rulesList? It looks like the list of your rules but it would be what the find action is running against. Presumably it should be List<RulesEntity>. I notice you're using a combination of ToUpper and trim calls. Do you have null or empty values? Are you dealing with foreign characters at all? I would probably recommend using String.Compare instead, although you'd still need to trim if that is an issue.

    Set a breakpoint on your else block and run your code to replicate the issue. Once it fires you will have the exact row that is causing the problem. Using the Immediate window run the rule again to see what you get. My gut instinct, assuming you're enumerating RuleEntity objects, is that your blob variable isn't set properly causing you to get errors. You didn't post the higher level enumeration code so it is hard to say.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, May 14, 2018 2:38 PM
    Moderator
  • Read what you wrote and assume you don't know what ""


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, May 15, 2018 2:21 PM
  • Thanks for your detailed answer.  Yes, as you say rulesList is just a List<RuleRntity>. 

    I removed all the trim and upper functions and did it in the db layer and low and behold I have not been able to reproduce the error.  The code has now processed millions of rows and not failed once. 

    Really don't know but am beginning to suspect pilot error :-)

    Tuesday, May 15, 2018 3:04 PM