Firstly, im an old c programmer, so probably got some daft questions.
Have created a simple web page, with a simple gridview control on it.
I have a sql database which i have linked to using linq.
Ive created a nice pictorial view of my tables using the server explorer and this has created default classes for each table.
I can simply create my linq query, and bind it to the control, this works fine, except, i would like to push the linq code into a class firstly to tidy up my code, and secondly to reuse as its called from a few different places.
My first attempt partially succeded, shove the linq code into its own static class, it tuns, but i cant return the results of the query easily.
Whats the simplist way to do this?
Thanks,
Jim
Loading
Scott LyslePosted Jun 22, 2008, 7:47 AM
Jim:
I am not sure what you mean by saying that you cannot return the results of the query easily. I tend to do what you have described in that, along with the dbml, I create a set of static accessor and mutator methods that are used to return types lists, full tables, or single results based upon a passed in argument item or list. For example, using the Northwind database, if I wanted to return a list of orders by order ID, I would create an accessor method such as this:
public static List<Order> GetOrdersById(int orderId)
{
NorthWindDataClassesDataContext dc = new
NorthWindDataClassesDataContext();
return (from ord in dc.GetTable<Order>()
where (ord.OrderID == orderId)
select ord).ToList<Order>();
}
The method returns a List of Orders which can then be bound to a grid.