0

I am going through a tutorial for asp.net and C# and the author used some code that I am trying to understand. I have an idea of what it does, but I just wanted to make sure.

 public IQueryable<Category> GetCategories()
    {
        var db = new WingtipToys.Models.ProductContext();
        IQueryable<Category> query = db.Categories;
        return query;
    }

I am a little confused by

 IQueryable<Category>

as well as the line

IQueryable<Category> query = db.Categories;

What I think it does is that it is a method called GetCategories() that only accepts Category objects. The method defines a new instance productcontext, it then runs some type of query and returns it. I could be close or way off. I just want to make sure.

Jake Funchion
  • 73
  • 3
  • 8
  • Maybe this SQL statement will help you understand what is being returned there: "select category from products" - The result is a list of categories from table products. LINQ to SQL allows you to write a query in c# with linq api and it will be transformed to SQL. :) – dev hedgehog Nov 10 '13 at 00:50
  • This code leaks a database connection that won't close until the garbage collector gets around to it. – Matthew Nov 10 '13 at 00:51
  • @Jake I suspect you are getting thrown by the generic syntax. `IQueryable` is an instance of the `IQueryable` interface that is specialized to work with `Category` objects, the same as `List` is a `List` that only works with `Category` objects. – Michael Edenfield Nov 10 '13 at 00:52

3 Answers3

1

... method called GetCategories() that only accepts Category objects ...

IQueryable<Category> is a type just like intor string so the method accepts nothing, it judt returns categories in a queryable way (IQueryable).

Xaqron
  • 29,931
  • 42
  • 140
  • 205
1

The IQueryable return type is used to indicate you want C# to pass along any filters that you use on the GetCategories() method to be passed directly to the database. Even though the method is setup to pass all categories back, you can limit them at a higher level. This saves you from having to create multiple methods for each filter you can think of that goes back to the database.

Here is link to a StackOverflow Example about the differences between IEnumerable and IQueryable

Community
  • 1
  • 1
Kalpers
  • 658
  • 5
  • 11
1

So I think the part you're having trouble with is the generic return type in the method signature, it can be a strange syntax if you haven't come across it before.

Your method is returning a type of IQueryable<Category>, so you're going to be returning an object that implements the interface IQueryable<T>, specifically one which is implemented to work specifically with Category objects.

If you wanted to create a list of strings, you'd do something like:

MyStrings = new List<string>();

And if you wanted to create a method to return a list of strings:

public List<string> FetchMyStrings(); // List<string> is your return type.

In your case, you're returning a list of Category objects:

public List<Category> FetchMyCategories(); // List<Category> is your return type.

For some excellent explanations of IQueryable<T>, and why you'd be interested in it, I'd recommend the answers here: What is the difference between IQueryable<T> and IEnumerable<T>?

The rest of the code seems to initialise a new database connection, and returns a queryable object, allowing you to get at the categories for use elsewhere in your code.

Community
  • 1
  • 1
Chris
  • 8,268
  • 3
  • 33
  • 46
  • Your information is a little wrong in the 2nd paragraph, there is a big difference between [`IQueryable`](http://msdn.microsoft.com/en-us/library/system.linq.iqueryable%28v=vs.110%29.aspx) and [`IQueryable`](http://msdn.microsoft.com/en-us/library/bb351562%28v=vs.110%29.aspx). – Scott Chamberlain Nov 10 '13 at 01:17
  • `IQueryable` vs `IQueryable` is not a big deal because `IQueryable` implments `IQueryable`. However one situation in the framework where I do know it is a big deal is [`IList`](http://msdn.microsoft.com/en-us/library/system.collections.ilist%28v=vs.110%29.aspx) and [`IList`](http://msdn.microsoft.com/en-us/library/5y536ey6%28v=vs.110%29.aspx). If you go look `IList` ***does not*** implement `IList`, so you can not use them as interchangeably as something that does. – Scott Chamberlain Nov 10 '13 at 01:25
  • I see, thanks for the tip; it's exactly why I have a stab at questions here =D – Chris Nov 10 '13 at 01:47