Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This foreach is producing error.

C#
var query1 = Mystudentities.Student.Select(x=>new
{
  a=x.a,
  b=x.b,
  c=x.c
});

var query2 = Mystudentities.Engineer.Select(x=>new
{
  a=x.a,
  b=x.b,
  c=x.c
});

var inter=query1.Intersect(query2);

foreach( var x in inter)
{
    a=x.a,
    b=x.b,
    c=x.c,
    d=1,
    e=2

}


What I have tried:

C#
var query1 = Mystudentities.Student.Select(x=>new
{
  a=x.a,
  b=x.b,
  c=x.c
});

var query2 = Mystudentities.Engineer.Select(x=>new
{
  a=x.a,
  b=x.b,
  c=x.c
});

var inter=query1.intersect(query2);

foreach( var x in inter)
{
    a=x.a,
    b=x.b,
    c=x.c,
    d=1,
    e=2
}


How to inter loop in foreach?
Posted
Updated 10-Dec-19 20:43pm
v3
Comments
Afzaal Ahmad Zeeshan 11-Dec-19 2:44am    
Where exactly you get the error?

I am doubting that you should be getting the error inside the last foreach because a = x.a, b = x.b,... is invalid.

Try:


foreach( var x in inter) // this is okay.
{
a=x.a;
b=x.b;
c=x.c;
d=1; // might need to use var d = 1;
e=2; // might need to use var e = 2;
}
You need to check more on the C# language grammar to understand how these are processed.

1 solution

Hello,
Try to add
var inter=query1.intersect(query2).ToArray();

like this and then use foreach like:
foreach (var oParam in objList)
{
    Console.WriteLine(oParam.UserID);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900