Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13582

Suitable type to Store Linq result in order to be able to query it later

I'm using linq to sql in my project. In Form1 I query a table like:

var dc = new Data.MyDataContext();
var query = from c in dc.Customers select c;  
program.MainForm.query = query;

Or in Form2 I query a table like:

var dc = new Data.MyDataContext();
var query = from b in dc.Banks select b;  
program.MainForm.query = query;

And I want to do something like the following later in MainForm:

this.DataGridView1.DataSource = this.query.Where(item=>item.ID < 10);

I don't know how to store the object. I tried storing it as an IQueryable but I was not able to query it because it is an Interface.

I want to store results from different tables in query, what is the simple way to do so? I think Generics can help but I don't know how.

Upvotes: 0

Views: 127

Answers (1)

Avish
Avish

Reputation: 4626

If you want to hold on to the query you'll need a field of type IQueryable<T> with T being the type of object queried.

If you want to hold on to the result of the query (e.g. so you can search inside it in RAM without fetching the data again), you'll need some materialized data structure like IList<T>.

Both options are problematic because they require type arguments, and UserControls can't be generic. I guess you could work with IQueryable<object> and IList<object> but then you won't be able to do much with the resulting objects since the control will know nothing about their type.

As an aside, as @SergeyBerezovskiy noted, it's also probably not a great idea to mix data access and presentation logic like this.

Upvotes: 3

Related Questions