开发者

LINQ: No support for passing var as method parameter

开发者 https://www.devze.com 2023-02-14 16:35 出处:网络
I want to encapsulate the following query into a method that returns a query result (i.e. members). I then want to take another query that will query off of that returned result.

I want to encapsulate the following query into a method that returns a query result (i.e. members).

I then want to take another query that will query off of that returned result.


I learned that I cannot use var as a parameter type or result type within the method that I want to create. I really wanted to have this support to make my life simple.

Additional details: MembersItemsControl.Items property has a private member '_ItemsSourceAsList' that is of type 'Users' which is a class that inherits from an ObservableCollection.


Any suggestions?


The qu开发者_如何学编程ery code is below:


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);


You can always pass LINQ collection as 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;
}

Where Member is type of items in MembersItemsControl.Items.


Do you mean you just want to me able to go var members = GetQuery();?

You can just treat the results as an 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;
}

Then you can use that query however you like:

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


You just have to explicitly type your variable members - var is just a shortcut to allow for implicitly typed local variables. If you do have to pass the variable, or return it outside the local scope, you'll have to use an explicitly typed variable - you can find out the type of your variable by hovering over it with your mouse(or look at it with the debugger) - it will tell you the full type you'll have to use.

0

精彩评论

暂无评论...
验证码 换一张
取 消