Filtering LINQ query into another 'var' variable [modified]
-
Hi all, I just trying get limited no'of columns from an var(Linq) query output. For example,
var query = from t in dc.Table_Name select new { t.id,t.name,t.description};
The above query output which contains 3 columns in each row. I wants to get the first column say 'id' into another variable. like :
var query_filter = from t in query select t.id;
but its giving error : the result can't be enumerated morethan once. Please suggest me how to get that. Thanks in advance, cheers sekhar
modified on Thursday, February 26, 2009 4:22 AM
-
Hi all, I just trying get limited no'of columns from an var(Linq) query output. For example,
var query = from t in dc.Table_Name select new { t.id,t.name,t.description};
The above query output which contains 3 columns in each row. I wants to get the first column say 'id' into another variable. like :
var query_filter = from t in query select t.id;
but its giving error : the result can't be enumerated morethan once. Please suggest me how to get that. Thanks in advance, cheers sekhar
modified on Thursday, February 26, 2009 4:22 AM
-
You shouldn't have to use First or FirstOrDefault. When you create a linq query, the result you get out (assuming your not running an aggregate function or First/Last/FirstOrDefault/LastOrDefault) is not the actual results of the query...its ultimately and IEnumerable that defines what the query should return when it is enumerated. You should be able to apply multiple modifications of your query before you actually iterate it:
var query = from t in dc.Table_Name select new { t.id, t.name, t.description };
IEnumerable queryOfID = from t in query select t.ID;foreach (int id in queryOfID)
{
Console.WriteLine(id);
}The above should work. The only scenario where it may not work is if you iterate over query once, then try to create queryOfID, and iterate over that. Even with LINQ to SQL, iterating over a query that returns data from a database more than once should work, however there may be certain scenarios where subsequent iterations may be prevented to prevent unexpected modification of a database (would depend...not sure of an example scenario right now that might cause that to happen).
-
You shouldn't have to use First or FirstOrDefault. When you create a linq query, the result you get out (assuming your not running an aggregate function or First/Last/FirstOrDefault/LastOrDefault) is not the actual results of the query...its ultimately and IEnumerable that defines what the query should return when it is enumerated. You should be able to apply multiple modifications of your query before you actually iterate it:
var query = from t in dc.Table_Name select new { t.id, t.name, t.description };
IEnumerable queryOfID = from t in query select t.ID;foreach (int id in queryOfID)
{
Console.WriteLine(id);
}The above should work. The only scenario where it may not work is if you iterate over query once, then try to create queryOfID, and iterate over that. Even with LINQ to SQL, iterating over a query that returns data from a database more than once should work, however there may be certain scenarios where subsequent iterations may be prevented to prevent unexpected modification of a database (would depend...not sure of an example scenario right now that might cause that to happen).
Thanks for your suggestion
-
Hi all, I just trying get limited no'of columns from an var(Linq) query output. For example,
var query = from t in dc.Table_Name select new { t.id,t.name,t.description};
The above query output which contains 3 columns in each row. I wants to get the first column say 'id' into another variable. like :
var query_filter = from t in query select t.id;
but its giving error : the result can't be enumerated morethan once. Please suggest me how to get that. Thanks in advance, cheers sekhar
modified on Thursday, February 26, 2009 4:22 AM
Why dont you use this query? var result = from t in query Select new("id"); Thats it,. I might have mistaken in synatx near new . . .But its just something like that. . And you will get only one column . .