user489041
user489041

Reputation: 28312

C# Syntax Issue

I have the following C# code:

private bool MailBoxAlreadyExists(string userEmail, Runspace runSpace)
{
    Pipeline pipeLine = runSpace.CreatePipeline();
    Command createMailBox = new Command("Get-User");
    createMailBox.Parameters.Add("identity", userEmail);
    pipeLine.Commands.Add(createMailBox);
    Collection user = pipeLine.Invoke();
    PSMemberInfo item = user[0].Properties.Where(property => property.Name == "RecipientType").SingleOrDefault();
    if (item != null)
    {
        if (string.Equals(item.Value.ToString(), "UserMailbox", StringComparison.OrdinalIgnoreCase))
        {
            pipeLine.Dispose();
            return true;
        }
        else
        {
            pipeLine.Dispose();
            return false;
        }
    }
    return false;
}

Im looking at the PSMemberInfo item = user[0].Properties.Where(property => property.Name == "RecipientType").SingleOrDefault(); line of code. Im not exactly sure what is going on in the Where(property => property.Name == "RecipientType") part of the statement. I think this is some type of lambda expression or something. Could someone explain this?

Second part of the question is, this code is underlined in Visual Studio. I get the following error from it:

Error   2   'System.Management.Automation.PSMemberInfoCollection' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Management.Automation.PSMemberInfoCollection' could be found (are you missing a using directive or an assembly reference?) C:\Users\dvargo\Documents\Visual Studio 2010\Projects\DVLib\ActiveDirectory\ADRunner.cs 566 33  ActiveDirectory

So to preform this kind of operation do I need another Assembly reference?

Upvotes: 0

Views: 310

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1503479

The => part is a lambda expression. A lambda expression can be converted into a delegate or an expression tree. It's like a better version of anonymous methods which were introduced in C# 2.

The Where method is part of LINQ. In this case you probably mean the LINQ to Objects version, Enumerable.Where - but it's not entirely clear as we don't know about the types involved. I strongly recommend that you start learning LINQ from a book or good tutorial - it isn't really the kind of topic which can be learned easily from a Q&A site such as Stack Overflow.

Your error is probably due to one of three problems:

  • You may not be using .NET 3.5, which is a pre-requisite for LINQ unless you use a third party library such as LINQBridge.
  • You may not have a using directive of using System.Linq; in your code
  • PSMemberInfoCollection may not implement the generic IEnumerable<T> interface. We can't really tell that part from what you've presented.

If you can give us more information, we may be able to help more.

Upvotes: 2

Shadow Wizzard
Shadow Wizzard

Reputation: 66398

This is Lambda expression indeed, which is part of LINQ.

The Where method is is part of LINQ, not Lambda expression by itself.

To be able to use it, add such line on top of your page:

using System.Linq;

You need the machine to have .NET 3.5 or higher installed, and the project in Visual Studio to target that framework or higher as well.

Upvotes: 3

Jacob Krall
Jacob Krall

Reputation: 28835

.Where and .SingleOrDefault are both in the System.Linq namespace, so you will have to add using System.Linq; to the top of the file.

In C#, the => operator denotes a lambda expression. A lambda is fairly simple to understand: it is an anonymous function, so the lambda

property => property.Name == "RecipientType"

is a function that takes an (implicitly typed) parameter property and returns the boolean expression property.Name == "RecipientType". A function that takes an object and returns a boolean is also known as a predicate.

Upvotes: 3

Related Questions