views:

1326

answers:

2

Anyone have a good example of setting up a LinqDataSource entirely in code? I don't need help writing the LINQ query. I just need help setting up the flow of the code. The reason I want to do it in code is because the complexity of the query I need goes beyond the capabilities of the LinqDataSource wizard.

+3  A: 

Well, can you specify what you mean by setting up? This is an example of how to create a LinqDataSource and prepare it for use:

LinqDataSource source = new LinqDataSource();
source.ContextTypeName = "MyDataContext";
source.TableName = "MyTable";
source.Select = "new (Id As MyId, Name As MyName)";
source.Where = "Id > 1";

To construct the query programatically instead, you can do this:

LinqDataSource source = new LinqDataSource();
source.ContextTypeName = "MyDataContext";
source.Selecting += source_Selecting;
...
void source_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    e.Result = from number in numbers where number > 1 select number;
}
bzlm
I was looking for a way to write a full linq query (not having to specify a TableName property, etc.) I would consider just writing a linq query and using the result as the datasource for my control and binding to it, but I want to take advantage of the paging/sorting that LinqDataSource has.
Brian David Berman
I've added an example of a custom query.
bzlm
A: 

This was made in relation SharePoint and the SPGridView but it might help you.

Johan Leino