3

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.

Mihai Limbășan
  • 64,368
  • 4
  • 48
  • 59
Brian David Berman
  • 7,514
  • 26
  • 77
  • 144

2 Answers2

3

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
  • 9,626
  • 6
  • 65
  • 92
  • 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 Feb 23 '09 at 21:04
  • I've added an example of a custom query. – bzlm Feb 24 '09 at 08:26
0

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

Johan Leino
  • 3,473
  • 1
  • 26
  • 27