Click here to Skip to main content
16,016,180 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i try to call query in forreach but it shows error

What I have tried:

C#
public static string Jqufunc(int yearP)
{
	string res = "[";
	ProjectdbEntities a = new ProjectdbEntities();
	var b = a.Catg_type;
	foreach (var c in b)
	{

		res += "'" + c.Catg_type1 + "',";
	}
	res = res.Substring(0, res.Length - 1);
	res += "]";

	var allprogs = a.Program_type;
	string res2 = "[";
	foreach (var pr in allprogs)
	{
		res2 += "{name: '" + pr.Prog_name + "',";
		var allcats = a.Catg_type;
		res2 += "data:[";


		var query = (a.Std_info.Join(a.Catg_type, stdtable => stdtable.Catg_id,
		catg => catg.Catg_id, (stdtable, catg) => new {stdtable, catg})
		.Join(a.Program_type, t => t.stdtable.Prog_id, prog => prog.Prog_id, (t, prog) => new {t, prog})
		.Join(a.Year_info, t => t.t.stdtable.year_id, yea => yea.year_id, (t, yea) => new {t, yea})
		.Where(t=>t.t.t.stdtable.year_id==yearP)
		.GroupBy(
		t =>
		new { t.t.t.catg.Catg_type1, t.t.prog.Prog_name, t.yea.year, t.t.t.stdtable.Catg_id },
		t => t.t.t.stdtable)
		.Select(g => new
		{
			g.Key.Catg_type1,
			g.Key.Prog_name,
			g.Key.year,
			g.Key.Catg_id,
			total_students = g.Select(p=>p.Catg_id).Distinct().Count()
		})).ToList();
		res2 = res2.Substring(0, res2.Length - 1);
		foreach(var ab in res2)
		{
			res2 += query(yearP);
		}
	}
	res2 += "]";
	res2 += "},";
	return res + "*" + res2;

}


error
'query' is a 'variable' but is used like a 'method'
Posted
Updated 17-May-16 13:14pm
v3

1 solution

Well...yes. It is.
Look at the message:
'query' is a 'variable' but is used like a 'method'

Then look at how you create and use query:
C#
var query = (a.Std_info.Join(a.Catg_type, stdtable => stdtable.Catg_id,
             ...
                    })).ToList();
...
res2 += query(yearP);
query is a List<T> of an anonymous type.
You can't use a list as a method!

I'm not sure what the heck you are trying to do here, but...it's not a method, yearP isn't shown, so it could be an index into the list in which case you'd want:
C#
res2 += query[yearP];
But that wouldn't work either because the anonymous type doesn't have a addition operator...
 
Share this answer
 
Comments
super_user 17-May-16 4:32am    
i cannot post complete code.. now please check updated question
super_user 17-May-16 4:32am    
yearp is a parameter
OriginalGriff 17-May-16 4:52am    
So are you trying to use it as an index? And if so, what value do you expect it to have? Bear in mind that C# indexes start at 0, so a variable called "yearP" I would expect to be 2016, 2015, ... - and likely way out of range of your collection!
F-ES Sitecore 17-May-16 4:57am    
Your query is too complicated to be bothered working out in my head, but I think you're going to need something like

query.FirstOrDefault(q => q.year == yearP);

that will get you the record from the list for that year, but you can't really add that as a string, you probably want to add one of it's properties. It's hard to work out what you intend on doing from the code.
OriginalGriff 17-May-16 5:25am    
I know the feeling!
That query is a good example of "just because you *can* do it, doesn't mean you *should* do it"...:laugh:

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