user3263515
user3263515

Reputation: 67

foreach loop is not running the code inside

I recently got asked to look at one of our C# web forms that you could search off of and it is supposed to return the result from our db when the user searches. I am usually a node developer but code is code and the task was handed to me.

I found the method where the code was running and I saw these ugly nested foreach loops so I set some breakpoints and to my surprise one of my foreachloops was breaking out of the loop before running the code inside it. I thought well I found it but I am not sure why this behavior is happening.

below is my full method I even put a try/catch around the foreach loop that was breaking out but there was no exception being thrown. so in my debugger the part that says

foreach (var b in bids)

all the code inside this foreach loop never executes and I have no idea why. Please can someone help

static public object GetData(DataManager value, string customer, string subdivision, string status, string user, string date, string tags, string serviceType, string pricingType)
    {
        var lst = new List<Bid_Master>();

        using (var context = new BiddingDataContext(KP.Common.KPConnectionString))
        {
            context.DeferredLoadingEnabled = false;

            var fdate = DateTime.Parse(date + " 00:00:00");
            var tdate = DateTime.Parse(date + " 23:59:59");

            var bids = context.Bid_Masters
                              .Where(x =>
                                        (x.Customer_ID == customer || customer == "-1") &&
                                        (x.Subdivision_ID == subdivision || subdivision == "-1") &&
                                        (x.Bid_Status == status || status == "-1") &&
                                        (x.Created_By == user || user == "-1") &&
                                        (x.Pricing_Type.ToString() == pricingType || pricingType == "ALL") &&
                                        ((x.Created_Date > fdate && x.Created_Date < tdate) || date == "01/01/2000") &&
                                        (x.Service_Type == serviceType || serviceType == "-1"))
                                .OrderByDescending(x => x.Created_Date);
            try
            {
                foreach (var b in bids)
                {
                    if (String.IsNullOrEmpty(tags))
                        lst.Add(b);
                    else
                    {
                        var arrtags = tags.Split(',');

                        var attrs = context.Bid_Master_Attributes.Where(x => x.BM_ID == b.BM_ID).ToList();

                        foreach (var attr in attrs)
                        {

                            foreach (var t in arrtags)
                            {
                                if (lst.FirstOrDefault(x => x.BM_ID == b.BM_ID) != null) continue;

                                var isDec = false;

                                decimal dVal = 0;
                                if (decimal.TryParse(t, out dVal)) isDec = true;

                                string sVal = t;

                                if ((isDec && attr.TagValueNum.HasValue && attr.TagValueNum == dVal) || sVal == attr.TagValueString)
                                {
                                    lst.Add(b);
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }

        var enumerable = lst.AsEnumerable();
        Syncfusion.JavaScript.DataSources.DataOperations operation = new Syncfusion.JavaScript.DataSources.DataOperations();




        if (value.Sorted != null && value.Sorted.Count > 0)
        {
            enumerable = operation.PerformSorting(enumerable, value.Sorted) as IEnumerable<Bid_Master>;
        }
        if (value.Where != null && value.Where.Count > 0) //Filtering
        {
            enumerable = operation.PerformWhereFilter(enumerable, value.Where, value.Where[0].Operator) as IEnumerable<Bid_Master>;
        }
        int count = enumerable.AsQueryable().Count();
        if (value.Skip != 0) //Paging
        {
            enumerable = operation.PerformSkip(enumerable, value.Skip) as IEnumerable<Bid_Master>;
        }
        if (value.Take != 0)
        {
            enumerable = operation.PerformTake(enumerable, value.Take) as IEnumerable<Bid_Master>;
        }

        return new
        {
            result = enumerable,
            count = count
        };
    }

Upvotes: 0

Views: 2150

Answers (2)

A. Gopal Reddy
A. Gopal Reddy

Reputation: 380

The bids list is not having any items to loop through. Try to run the query directly in the Database and check whether any output is coming or not.

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61339

Boy that code is ugly. The only "jump" line that would do what you described (I assume you understand if statements) I found was here

 foreach (var t in arrtags)
 {
      if (lst.FirstOrDefault(x => x.BM_ID == b.BM_ID) != null) continue;

The continue statement means: "Skip to the next iteration of the loop". In this case, the next element in arrtags.

If all the code in the foreach is not running (not just part of it) make sure bids is actually populated.

Upvotes: 1

Related Questions