Passing Linq query results to a different function
-
Hi, I have the following linq code within my app var results = (from t in query where t.ProjectID == new Guid(this.Session["ProjectID"].ToString()) orderby t.Number ascending select new { ID = t.ID, Tag = t.Number, Description = t.Description, SubSystems = GeneralHelper.TagSubSystems(t), Module = t.Area.Module.Name, Discipline = t.Discipline.Name, ITRs = GeneralHelper.TagITRsHTML(t) }); I want to pass the query result to another function to handle my paging. I currently have another function defined like this public void BindGridData(IQueryable result) { gridView.DataSource = result; gridView.DataBind(); } It works like this, but the problem is I cannot do "take" or "skip" on "result" because it's declared as IQueryable. Is there a work around to handle this? Thanks
-
Hi, I have the following linq code within my app var results = (from t in query where t.ProjectID == new Guid(this.Session["ProjectID"].ToString()) orderby t.Number ascending select new { ID = t.ID, Tag = t.Number, Description = t.Description, SubSystems = GeneralHelper.TagSubSystems(t), Module = t.Area.Module.Name, Discipline = t.Discipline.Name, ITRs = GeneralHelper.TagITRsHTML(t) }); I want to pass the query result to another function to handle my paging. I currently have another function defined like this public void BindGridData(IQueryable result) { gridView.DataSource = result; gridView.DataBind(); } It works like this, but the problem is I cannot do "take" or "skip" on "result" because it's declared as IQueryable. Is there a work around to handle this? Thanks
convert the linq result to list
var results = (from t in query
where t.ProjectID == new Guid(this.Session["ProjectID"].ToString())
orderby t.Number ascending
select new
{
ID = t.ID,
Tag = t.Number,
Description = t.Description,
SubSystems = GeneralHelper.TagSubSystems(t),
Module = t.Area.Module.Name,
Discipline = t.Discipline.Name,
ITRs = GeneralHelper.TagITRsHTML(t)
}).ToList(); -
convert the linq result to list
var results = (from t in query
where t.ProjectID == new Guid(this.Session["ProjectID"].ToString())
orderby t.Number ascending
select new
{
ID = t.ID,
Tag = t.Number,
Description = t.Description,
SubSystems = GeneralHelper.TagSubSystems(t),
Module = t.Area.Module.Name,
Discipline = t.Discipline.Name,
ITRs = GeneralHelper.TagITRsHTML(t)
}).ToList(); -
public void BindGridData(IEnumerable result)
{
gridView.DataSource = result;
gridView.DataBind();
}Also note the use of the pre tags to properly format the code snippet
I know the language. I've read a book. - _Madmatt
-
public void BindGridData(IEnumerable result)
{
gridView.DataSource = result;
gridView.DataBind();
}Also note the use of the pre tags to properly format the code snippet
I know the language. I've read a book. - _Madmatt
-
Create a structure to represent your query results, instead of using anonymous types. Then you know what kind of IEnumerable it is. Alternatively, make your Bind function generic, like so:
public void BindGridData<T>(IEnumerable<T> result)
{
gridView.DataSource = result;
gridView.DataBind();
}Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Create a structure to represent your query results, instead of using anonymous types. Then you know what kind of IEnumerable it is. Alternatively, make your Bind function generic, like so:
public void BindGridData<T>(IEnumerable<T> result)
{
gridView.DataSource = result;
gridView.DataBind();
}Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
IEnumerable<T> requires a type but IEnumerable does not.
I know the language. I've read a book. - _Madmatt
modified on Tuesday, August 31, 2010 12:12 PM
-
Create a structure to represent your query results, instead of using anonymous types. Then you know what kind of IEnumerable it is. Alternatively, make your Bind function generic, like so:
public void BindGridData<T>(IEnumerable<T> result)
{
gridView.DataSource = result;
gridView.DataBind();
}Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Although I agree a concrete type is better for readability and good design it isn't necessary in order to pass the query to a method.
I know the language. I've read a book. - _Madmatt
-
Although I agree a concrete type is better for readability and good design it isn't necessary in order to pass the query to a method.
I know the language. I've read a book. - _Madmatt
-
IEnumerable<T> requires a type but IEnumerable does not.
I know the language. I've read a book. - _Madmatt
modified on Tuesday, August 31, 2010 12:12 PM
-
Sorry, the brackets didn't render. Fix it
I know the language. I've read a book. - _Madmatt
-
the reason i didnt define a concrete type is because the function will be accepting queries from different classes
Ok, then the second solution is best :) Oh, and you don't need to do a ToList() if you're going that route. Linq results are all IEnumerable.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
the reason i didnt define a concrete type is because the function will be accepting queries from different classes
Using generics, as Ian has suggested, doesn't preclude you from doing that. You would just need to create the class/struct for each query, which from a architectural perspective isn't a bad idea. However, if you have many different queries that are possible then perhaps you need to improve your design, one size fits all implementation are very difficult to maintain.
I know the language. I've read a book. - _Madmatt
-
Create a structure to represent your query results, instead of using anonymous types. Then you know what kind of IEnumerable it is. Alternatively, make your Bind function generic, like so:
public void BindGridData<T>(IEnumerable<T> result)
{
gridView.DataSource = result;
gridView.DataBind();
}Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)One more thing, lets say I store "result" in session variable like this public void BindGridData(IEnumerable result) { Session["result"] = result; gridView.DataSource = result; gridView.DataBind(); } and then I want to call this function within another event, how do I call it? how do I cast Session["result"]? Thanks
-
One more thing, lets say I store "result" in session variable like this public void BindGridData(IEnumerable result) { Session["result"] = result; gridView.DataSource = result; gridView.DataBind(); } and then I want to call this function within another event, how do I call it? how do I cast Session["result"]? Thanks
First of all don't store it in session state. Remember this is the result of your query, not the query itself, and could be very large. It's also quite clear how to cast it. It is being passed into your method as an IEnumerable, guess what it should be cast as?
I know the language. I've read a book. - _Madmatt