views:

1155

answers:

4

I have a pretty complicated Linq query that I can't seem to get into a LinqDataSsource for use in a GridView:

IEnumerable<ticket> tikPart = (
    from p in db.comments where 
        p.submitter == me.id && 
        p.ticket.closed == DateTime.Parse("1/1/2001") && 
        p.ticket.originating_group != me.sub_unit 
    select p.ticket
    ).Distinct();

How can I get this into a GridView? Thank you!

+3  A: 
gridview.DataSource = tikPart.ToList();
gridview.DataBind();
leppie
Wow... that is so simple when I see it.Is there any way to get this into a DataSource so I can use the sorting of the GridVIew?
naspinski
You could use an object datasource and have it bound to a method that returns that query. I never use the LinqDatasource so i cant help there
mattlant
You have to do sorting and paging yourself when doing the method i mentioned. It might be easier using matts suggestion above, with LinqDataSource.
leppie
If you want sorting and paging a LinqToSqlDataSource or an ObjectDataSource are the best ways, otherwise you have to use on-the-fly expression generation which can get ugly (as my boss found out!)
Slace
I wish I could accept more than one, thank you.
naspinski
A: 

You can setup your Gridview with no Datasource. Setup the gridview columns, and in codebehind bind that result to the grid view.

mattlant
Thanks for the input! Do you have any sort of example where I could see this? I understand the concept, but an example would help...
naspinski
Its fairly straight forward. Create a class with a method such as GetData(). Add an objectdatasource to your page. use the smart tag to set it up, and you only need the select query if displaying data. So setup that to call the classes method you just created.
mattlant
awesome, thanks you so much!
naspinski
+2  A: 

@leppie - There is no need to call a ToList() on the IQueryable when attaching it as a data source.

Provided your DataContext has not been disposed of prior to the DataBind method being called ToList is a redundant call.

By default a DataContext uses lazy-loading, so that the data is only fetched from the database when the IQueryable is Enumerated. ToList() performs an Enumeration and does the call, so does DataBind().

So you can do something like this:

using(MyDataContext ctx = new MyDataContext(){
  this.MyGridView.DataSource = from something in ctx.Somethings where something.SomeProperty == someValue select something;
  this.MyGridView.DataBind();
}

Depending on how your disposing your DataContext determins what to bind to a data source.

You can then either use auto generated columns on the GridView, so that every property in your returned object is turned into a column, or you can write the columns with the designer and set up the binding rules there.

Slace
Thanks for the extra info. I was still under the impression DataSource property needed an IList :)
leppie
I wish I could accept more than one, thank you.
naspinski
leppie - You only need to make sure that your data source implemented IEnumerable. If you check what IList and IQueryable implement you can trace it all the way back to IEnumerable :)
Slace
A: 

You can bind IQueryable<> type to GridView using LinqDataSource control. http://johnsobrepena.blogspot.com/2010/01/data-bind-coolgridview-to-iqueryable.html

John