LINQ:不支持将var作为方法参数传递

时间:2011-03-04 14:34:59

标签: c# linq

我想将以下查询封装到返回查询结果的方法中(即成员)。

然后我想采取另一个查询来查询返回的结果。


我了解到我不能在我想要创建的方法中使用var作为参数类型或结果类型。我真的很想得到这种支持,让我的生活变得简单。

其他细节: MembersItemsControl.Items属性有一个私有成员'_ItemsSourceAsList',类型为'Users',它是一个继承自ObservableCollection的类。


有什么建议吗?


查询代码如下:


var members = (from member in MembersItemsControl.Items
               where
               (
                   // Match either male or female selection
                   (member as UserInformation).sex.Equals("Male") == 
                       SeekingMale.IsChecked.Value &&
                   (member as UserInformation).sex.Equals("Female") == 
                       SeekingFemale.IsChecked.Value
               )
               ||
               (
                    // Provide both male and female if both options are selected
                    SeekingMale.IsChecked.Value == true &&
                    SeekingFemale.IsChecked.Value == true
               )
               select member);

3 个答案:

答案 0 :(得分:3)

您始终可以将LINQ集合作为IEnumerable<Member>传递。

public IEnumarable<Member> Foo()
{
    var members = (from member in MembersItemsControl.Items
                   where
                   (
                       // Match either male or female selection
                       (member as UserInformation).sex.Equals("Male") == 
                           SeekingMale.IsChecked.Value &&
                       (member as UserInformation).sex.Equals("Female") == 
                           SeekingFemale.IsChecked.Value
                   )
                   ||
                   (
                        // Provide both male and female if both options are selected
                        SeekingMale.IsChecked.Value == true &&
                        SeekingFemale.IsChecked.Value == true
                   )
                   select member);

    return members;
}

其中MemberMembersItemsControl.Items中的项目类型。

答案 1 :(得分:1)

你的意思是你只想让我能够去var members = GetQuery();吗?

您可以将结果视为IEnumerable<Member>

private IEnumerable<Member> GetMemberQuery()
{
    var members = (from member in MembersItemsControl.Items
                   where
                   (
                       // Match either male or female selection
                       (member as UserInformation).sex.Equals("Male") == 
                           SeekingMale.IsChecked.Value &&
                       (member as UserInformation).sex.Equals("Female") == 
                           SeekingFemale.IsChecked.Value
                   )
                   ||
                   (
                       // Provide both male and female if both options are selected
                       SeekingMale.IsChecked.Value == true &&
                       SeekingFemale.IsChecked.Value == true
                   )
                   select member);

    return members;
}

然后您可以使用该查询:

var otherQuery = GetMemberQuery().Where(x=> x.Name == "Bob");

答案 2 :(得分:1)

您只需要显式键入变量members - var is just a shortcut以允许隐式输入的局部变量。如果您必须传递变量,或者将其返回到局部范围之外,则必须使用显式类型变量 - 您可以通过用鼠标将变量悬停在变量上来查找变量的类型(或者查看变量的类型)调试器) - 它会告诉你你必须使用的完整类型。