0

I have a trying to select a record. In db, i have two records for Month 1 and 4.

I need to get values for both month 1 and 4, but all i get is value for month 1 both times in iteration (Month 1 and Month 4).

EX: In dB Month 1 Value is : 55 and Month 4 value is 22, but i get value 55 for both Months 1 and 4, while iteration in code.

  for (var month = 1; month <= 12; month++)
            {
    var itemMonth = month;
    var itemAuditReport = proxy.itemAuditReports.SingleOrDefault(i => i.SKU == itemSku && i.Month == itemMonth && i.Year==itemYear);
    //
    }
 if (itemAuditReport == null)
                    {
// Do Something
}

Am i missing something ?

Nanu
  • 3,010
  • 10
  • 38
  • 52
  • 1
    your code is fine. double check your data or run the following sql to see if you get the same result. select * from itemAuditReport i where i.SKU=itemSku && i.Month=1 && i.Year=itemYear; select * from itemAuditReport i where i.SKU=itemSku && i.Month=4 && i.Year=itemYear; – Ray Cheng Apr 26 '12 at 18:25
  • If i run the code by explicitly specifying the month as 1 and 4, i get the correct different results. So definitely something is wrong with the code. – Nanu Apr 26 '12 at 23:38

4 Answers4

1

why dont you try out

I am guessing that itemAuditReportis the same item get assigned in iteration causing problem so make use of list and add items in it

List<Item> item = null;

for (var month = 1; month <= 12; month++)
{
 var itemMonth = month;
 var itemAuditReport = proxy.itemAuditReports.SingleOrDefault(i => i.SKU == itemSku && 
                       i.Month == itemMonth && i.Year==itemYear);
 if(itemAuditReport!=null)
   item.Add(itemAuditReport);
//
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • This solution will solve your problem, I ran into something like this before but for a different reason. I also suggest defining obvious types when you have the chance and leave var for ambiguous types. – John Sykor Apr 26 '12 at 19:04
  • I don't see how this is different from OP's code. The itemAuditReport will get the same result, but you are only checking it for null or not. OP's issue is that itemAuditReport is getting invalid results. – Ray Cheng Apr 27 '12 at 04:19
  • @RayCheng - i m pushing data in list not using that variable after fatching record...that might be cause of problem... – Pranay Rana Apr 27 '12 at 04:20
1

This will give you some insight on your error. Why is it bad to use an iteration variable in a lambda expression I will let you know if I find something to help you out fix the issue.

Community
  • 1
  • 1
John Sykor
  • 727
  • 5
  • 15
  • Did you find any examples ? I am still facing this issue. not sure how to resolve it – Nanu May 01 '12 at 16:56
1

Like John Sykor suggested, try this:

for (var month = 1; month <= 12; month++)
{
  var itemMonth = month;
  var year = itemYear;
  var sku = itemSku;
  var itemAuditReport = proxy.itemAuditReports.SingleOrDefault(i => 
      i.SKU == sku && i.Month == itemMonth && i.Year==year);
}

the following examples explain it more:

this outputs 10 ten times.

List<Action> actions = new List<Action>();
for (int i = 0; i < 10; i++)
{
    actions.Add(() => Console.WriteLine(i));
}
foreach (Action action in actions)
{
    action();
}

but the following outputs 0...9 as expected

List<Action> actions = new List<Action>();
for (int i = 0; i < 10; i++)
{
    var x = i; //<--important line
    actions.Add(() => Console.WriteLine(x));
}
foreach (Action action in actions)
{
    action();
}
Ray Cheng
  • 12,230
  • 14
  • 74
  • 137
0

Yes, the keys in the Entity Framework model wasn't set properly, Sku was set as a primary key with title. in the model, hence bringing the same result set.

I changed the keys to be applied on Sku, Month and Year, and it worked great !!

Nanu
  • 3,010
  • 10
  • 38
  • 52