This is a migrated thread and some comments may be shown as answers.

Linq - Entity Framework as datasource

6 Answers 133 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Al
Top achievements
Rank 1
Iron
Iron
Iron
Al asked on 23 Jun 2010, 03:51 PM
Hi,

Can you please point me to an example that uses linq data as a source for the grid in the scenario something like below:

 

 

 

 

protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)

 

{

    RadGrid1.DataSource =

 

this.GridSource2;

 

}

 

 

 

private object GridSource2

 

{

 

 

get

 

 

 

 

 

{

 

 

Object obj = this.ViewState["_gds"];

 

 

 

if (obj != null)

 

{

 

 

return obj;

 

}

 

 

else

 

 

 

 

 

{

 

 

string s;

 

 

 

 

 

 

 

 

 

 

object ss = from i in EntityConnection.INVENTORY select new { i.INV_DEVICENAME, i.INV_DEVICETYPE };

 

 

 

this.ViewState["_gds"] = ss;

 

 

 

return ss;

 

}

}

}

6 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 24 Jun 2010, 09:07 AM
Hi Al,

For an example how to use manual populate RadGrid control using LINQ context please refer to this online demo. Although LinqToSql context is used in the particular demo, same as demonstrated approach applies for EF too.

Best wishes,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 24 Jun 2010, 12:46 PM
Thanks Rosen that was a great help. This may just be my lack of understanding .NET but could you perhaps explain why code snippit #1 works but  #2 does not (I need to use #2):

************** #1 ***********************
protected
void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)

 

{

 

 

 

 

    RadGrid1.DataSource = from i in EntityConnection.INVENTORY select new { INV_IRN = i.INV_IRN, INV_DEVICENAME = i.INV_DEVICENAME, INV_DEVICETYPE = i.INV_DEVICETYPE };

 

    RadGrid1.MasterTableView.DataKeyNames =

 

new string[] { "INV_IRN" };

 

}
****************************************



 

 

 

**************** #2 *********************
private
object GridSource2

 

{

 

 

    get

 

    {

 

 

        Object obj = this.ViewState["_gds"];

 

 

 

        if (obj != null)

 

        {

 

 

            return obj;

 

        }

 

 

        else

 

        {

 

 

            object ss = from i in EntityConnection.INVENTORY select new { INV_IRN = i.INV_IRN, INV_DEVICENAME = i.INV_DEVICENAME, INV_DEVICETYPE = i.INV_DEVICETYPE };

 

 

 

            this.ViewState["_gds"] = ss;

 

 

 

            return ss;

 

        }

    }

}

 

 

protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)

 

{

RadGrid1.DataSource =

 

this.GridSource2;

 

RadGrid1.MasterTableView.DataKeyNames =

 

new string[] { "INV_IRN" };

 

}
****************************************

0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 28 Jun 2010, 08:06 AM
Hi,

Maybe I need to explain a little more about my post above. I can connect to the grid to a linq query just fine directly in the NeedDatasource event like this:

RadGrid1.DataSource = from i in EntityConnection.INVENTORY select new { INV_IRN = i.INV_IRN, INV_DEVICENAME = i.INV_DEVICENAME, INV_DEVICETYPE = i.INV_DEVICETYPE };

But when I use the same query in a separate routine that uses the 'ViewState' object as per the demos I get a 'serialisation' error. Please help asap as my grid is currently unusable
0
Rosen
Telerik team
answered on 28 Jun 2010, 12:19 PM
Hi Al,

I suspect that error you are facing is due to the fact that IQueryable<> returned from the entity context fails serialization. Therefore you should instead force query's execution first and then store the results. The easiest way to achieve this is by calling ToList() on the query.
 
Regards,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 28 Jun 2010, 12:37 PM
Thanks Rosen, I am also currently tracking this issue as a support ticket so will close this thread once I get the issue resolved there.
0
Al
Top achievements
Rank 1
Iron
Iron
Iron
answered on 30 Jun 2010, 10:04 AM
Thanks to the guys at Telerik I have reached a conclusion on this - it seems that while Linq query results can be persisted in the ViewState (with some complexity), it is actually not necessary. The results can be applied to the grid directly in the NeedDataSource event and it will all work just fine:

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {         
            RadGrid1.DataSource = from i in EntityConnection.INVENTORY select new testclass { INV_IRN = i.INV_IRN, INV_DEVICENAME = i.INV_DEVICENAME};
            RadGrid1.MasterTableView.DataKeyNames = new string[] { "INV_IRN" };
        }

Tags
Grid
Asked by
Al
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Rosen
Telerik team
Al
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or