C# 语法问题
我有以下 C# 代码:
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;
}
我正在查看 PSMemberInfo item = user[0].Properties.Where(property => property.Name == "RecipientType").SingleOrDefault();
行代码。我不太确定语句的 Where(property => property.Name == "RecipientType")
部分发生了什么。我认为这是某种类型的 lambda 表达式之类的。有人可以解释一下吗?
问题的第二部分是,这段代码在 Visual Studio 中带有下划线。我从中得到以下错误:
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
那么要执行这种操作,我需要另一个程序集引用吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.Where
和.SingleOrDefault
均位于System.Linq
命名空间中,因此您必须添加using System.Linq;< /code> 到文件顶部。
在 C# 中,
=>
运算符表示 lambda 表达式。 lambda 很容易理解:它是一个匿名函数,因此 lambda是一个接受(隐式类型化)参数
property
并返回布尔表达式property.Name == " 的函数收件人类型”
。接受对象并返回布尔值的函数也称为谓词。.Where
and.SingleOrDefault
are both in theSystem.Linq
namespace, so you will have to addusing 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 lambdais a function that takes an (implicitly typed) parameter
property
and returns the boolean expressionproperty.Name == "RecipientType"
. A function that takes an object and returns a boolean is also known as a predicate.这确实是 Lambda 表达式,它是 LINQ 的一部分。Where
方法是 LINQ,而不是 Lambda 表达式本身。为了能够使用它,请在页面顶部添加这样的行:
您需要计算机安装 .NET 3.5 或更高版本,并且 Visual Studio 中的项目也以该框架或更高版本为目标。
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:
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.
=>
部分是一个 lambda 表达式。 lambda 表达式可以转换为委托或< em>表达式树。它就像 C# 2 中引入的匿名方法的更好版本。Where
方法是 LINQ 的一部分。在这种情况下,您可能指的是 LINQ to Objects 版本,Enumerable.Where
- 但并不完全清楚,因为我们不知道所涉及的类型。我强烈建议您从书籍或优秀教程中开始学习 LINQ - 这并不是那种可以从 Stack Overflow 等问答网站轻松学习的主题。您的错误可能是由于以下三个问题之一造成的:
using System.Linq;
指令PSMemberInfoCollection
可能无法实现通用IEnumerable
接口。我们无法从您所呈现的内容中真正分辨出那部分。如果您能给我们提供更多信息,我们也许能够提供更多帮助。
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:
using System.Linq;
in your codePSMemberInfoCollection
may not implement the genericIEnumerable<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.