0
return db.Orders.Where(o => o.Customer == User.Identity.Name);

Where does o come from? Sometimes I see this form used with other letters like c? Is this exchangeable? Where does User.Identity.Name come from?

2
  • This isn't specific to Entity. Do a search on Lambda Expressions. That's what this is. But it's basically just a function. The 'o' is what you're passing into the function. Commented Jan 22, 2014 at 21:23
  • See this related question. Commented Jan 23, 2014 at 2:14

4 Answers 4

4

Where does o come from?

Where takes a Func delegate that takes an instance of whatever collection type you are using. In this case, it would be Func<Order, bool>. Your Lambda statement declares an anonymous function that matches the delegate. o is simply the Order input. You can name it whatever you like. If you wrote out the function in long-hand, it would look something like:

public bool AnonymousFunction(Order o)
{
    return o.Customer == User.Identity.Name;
}

If you're querying using LINQ to Objects, this delegate will be called for each of the elements to find the ones in the collection that match. If you're querying LINQ to SQL (or some other query provider), your delegate will be converted into an Expression Tree which will be used to generate the actual query syntax.

Where does User.Identity.Name come from?

I'm assuming you're inside of some sort of web application and User.Identity is pulling the logged in user information from the Forms Authentication token.

Sign up to request clarification or add additional context in comments.

Comments

2

The => operator is used to define a lambda statement - essentially an inline function. It's the inline equivalent of:

public bool Myfunc(Order o)
{
   return o.Customer == User.Identity.Name;
}

The type of o is inferred from the parameter type of the Func passed into Where. In this case, the type will be Order since IEnumerable<Order>.Where() takes a Func<Order, bool> with an Order as the input and a bool as the output.

The actual letter does not mater. A good convention is to use an identifier that is relatable to the source type, so o or order would be good choices.

User is probably a property of the class the function is in (i.e. Page), and User.Identity.Name is the name of the identity associated with the user.

Comments

1

This is known as Lambda Expression

By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls.

In your case, you are using

db.Orders.Where(o => o.Customer == User.Identity.Name);

In place of o you can use whatever character you want and that character will be behave as instance of collection type ie. here you are using collection of Orders, so o will behave as instance of Order class.

Comments

0

All the answers provided are correct. I'll just try to explain you in simple language about the statement:

You are accessing the Order table by writing db.Orders Then you are putting up a condition on the table using Where

Inside Where you specify the condition. This is done using lambda expression. Lambda expression is nothing but just a way of saying that I'll call each row as o(or may be anything, it can be x,y, abc, anything, its just a name) and for that(=>) check if o.Customer is equal to currently logged in User.

If yes, return me all those rows which match this condition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.