public void GetTeams()
{
var Teams = from sll in db.Sports_Leagues_Level
join nba in db.nba_Team_Names on sll.ID equals nba.SLLid
join league in db.Sports_Leagues on sll.Leagues equals league.Id
select new
{
SllId = sll.ID,
Team = nba.Abreviated_Long,
League = league.LeaguesAbr
};
foreach (var item in Teams)
{
Console.WriteLine("{0} {1} {2}", item.SllId, item.League, item.Team);
}
}
Sam HobbsPosted Jan 21, 2012, 6:43 PM
RichPosted Jan 21, 2012, 6:26 PM
Sam HobbsPosted Jan 19, 2012, 4:45 AM
{{{}{}{}{}}}RichPosted Jan 18, 2012, 7:21 PM
Sam HobbsPosted Jan 18, 2012, 5:04 PM
So does the code posted by Shen Hengbin not work?
RichPosted Jan 18, 2012, 2:54 PM
VulpesPosted Jan 18, 2012, 9:40 AM
Incidentally, I wrote an article last year about when you should use 'var':
http://www.c-sharpcorner.com/uploadfile/b942f9/when-should-you-use-the-var-keyword-in-C-Sharp/
Sam HobbsPosted Jan 18, 2012, 3:04 AM
The main problem is that you don't know what a var is actually, so you don't know what you can do with it. That is frustrating for me.
Another problem is that you cannot pass a var from a function; a var cannot be the return value from a function and a var cannot be a parameter of a function, not even as an "out" parameter.
Jignesh TrivediPosted Jan 17, 2012, 10:39 PM
I observe your code, I think it doesn't matter whether you use "var" key word or IEnarable. But donot use IQuerable in this code because your query has fixed where criteria and you don't farther filtration on your query. So my opinion is cast in to list.
var Teams = (from sll in db.Sports_Leagues_Level
join nba in db.nba_Team_Names on sll.ID equals nba.SLLid
join league in db.Sports_Leagues on sll.Leagues equals league.Id
select new Sports_Leagues_Level
{
SllId = sll.ID,
Team = nba.Abreviated_Long,
League = league.LeaguesAbr
}).ToList();
foreach (var item in Teams)
{
Console.WriteLine("{0} {1} {2}", item.SllId, item.League, item.Team);
}
If you use Iquerable here then, its query on your data base more than one time (equal to Teams.count()), so I think it decrease your code performance.
hope this help.
Shen HengbinPosted Jan 17, 2012, 8:48 PM
In my opinion I like the var keyword better.
And if you you really want to change the var keyword into the IQueryable
U could use the AsQueryable
For example (I think Sports_Leagues_Level is your class name ....):
IQueryable
join nba in db.nba_Team_Names on sll.ID equals nba.SLLid
join league in db.Sports_Leagues on sll.Leagues equals league.Id
select new Sports_Leagues_Level
{
SllId = sll.ID,
Team = nba.Abreviated_Long,
League = league.LeaguesAbr
}).AsQueryable