Solved

IQueryable

Posted on 2014-09-11
1
146 Views
Last Modified: 2016-02-18
If Someone could help me understand this piece of code bellow

public<Product> GetProducts([QueryString("id")] int? categoryId)
        {
            var _db = new WingtipToys.Models.ProductContext();
                IQueryable<Product> query = _db.Products;
                if (categoryId.HasValue && categoryId > 0)
                {
                    query = query.Where(p => p.CategoryID == categoryId);
                }
                return query;
        }

Open in new window

0
Comment
Question by:yguyon28
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40318481
You have a method named GetProducts, which takes in a (nullable) integer. This value comes from the querystring parameter named "id", which would be passed in a web request to your service/web page. Inside the method, you create a new database context which you will use to interact with your database. From the database, you will be retrieving information from the Products table. In your code, the Products table is represented as  an IQueryable of Product--vaguely like a list of products. An IQueryable just means that this is something that you can query with LINQ, and typically the data originates from a database. So you search through this list of products using the categoryId value that came in as a parameter. Wherever the category ID matches in a database record (from the IQueryable), you will return it from the method.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question