MVC with C#

Reviewing the sinppet on a ebook about MVC implementation and attach a code under Product Controller,

public ActionResult Details(int id)

{
using (var db = new ProductsDataContext())
{
var product = db.Products.SingleOrDefault(x => x.ProductID == id);
if (product == null)
return View("NotFound");
return View(product);
}
}

In (x => x.ProductID == id), does x is the parameter where expression after => is the assignment of a function ? Does I need to define x somewhere in the program ? I guess x refer to a Product object.  Tks
AXISHKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Yes, "x" is a variable declaration. The type of "x" is implicit in this example, but you could explicitly declare it:

e.g.

var product = db.Products.SingleOrDefault(Product x => x.ProductID == id);

Open in new window


So no, you do not need to declare "x" anywhere because that's exactly what that line is doing. Read up on lambda expressions.
0
AXISHKAuthor Commented:
Thanks but i still can't get it, how can x know that it represent a product object ? Can you explain more ? Tks
0
Fernando SotoRetiredCommented:
This is called a Lambda Expression and can be identified as such by the use of the Lambda operator, ->, a hyphen and immediately following it with a greater then sign. On the left side of the lambda operator is the input parameter, which in this case is the variable x and represents a single record from the database table Products. On the right side of the lambda operator is an expression which operates on the record. The query runs through every record in the table Products searching for the one who's ProductID field matches the id variable defined elsewhere in the code. The method SingleOrDefault will return a single record or no records but if more then one record is found an exception is thrown.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

AXISHKAuthor Commented:
Thanks but my question is how does x know that it represent Product object ,rather than Categoary object for example ? Where can it tell ??
0
Fernando SotoRetiredCommented:
When the statement is executed, var product = db.Products.SingleOrDefault(x => x.ProductID == id);, which is a Linq to Entity Framework query the SingleOrDefault method will go through each of the records passed to it and process it in the lambda expression. As each record is processed the next one come along until all the records have been processed. Think of it as a conveyor belt each record is placed on the conveyor belt and processed through all the methods in the query.
0
käµfm³d 👽Commented:
That's the implicit part that I mentioned above. SingleOrDefault is a generic method which operates on collections of things. The definition of SingleOrDefault expects that it should be called against an IEnumerable<T>, where T is a type parameter specified by you when you code. In your particular case, you've got and IEnumerable<Product>. So SingleOrDefault knows that the T in IEnumerable<T> is Product. Furthermore, the argument to SingleOrDefault expects a function "pointer" that takes in a Product parameter and returns a bool. The compiler can therefore implicitly determine that "x" should be of type Product. If you mouse over the "x" within the editor, then you will see "x" typed as such.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
To your question, "but my question is how does x know that it represent Product object ", OK, the variable db represents the mapping between your code and the database. The data that the query will be operating on is defined next which in this case is the Products table. The query will take one record at a time and pass it into SingleOrDefault method that input is the input to the lambda expression. This process continues until all records are processed.
0
AXISHKAuthor Commented:
Tks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.