-1

I'm trying to fill my DataGrid

dgGoals.ItemsSource = GetGoals(new int[] { 1, 2, 3 });

This is the In Memory object that has data loaded from a different process

static ObservableCollection<Goal> goals = new ObservableCollection<Goal>();

I tried using this example Linq version of SQL "IN" statement but both the lambda and LINQ statements are returning null when it should be 100 records.

public static ObservableCollection<Goal> GetGoals(int[] selectedGoalKey)
{
    //goals has 170 records at this point
    //selectedGoalKey has 3 items (1,2,3)
    //goals has 100 records with Goal_Key of 1,2 or 3

    //Returns null
    return goals.Where(f => selectedGoalKey.Contains(f.Goal_Key)) as ObservableCollection<Goal>;

    //Returns null
    return (from g in _Goals
            where selectedGoalKey.Contains(g.Goal_Key)
            select g) as ObservableCollection<Goal>;
}

EDIT Fixed and now works

public static IEnumerable<Goal> GetGoals(int[] selectedGoalKey)
{
    //goals has 170 records at this point
    //selectedGoalKey has 3 items (1,2,3)
    //goals has 100 records with Goal_Key of 1,2 or 3

    //Now returns 100 records
    return goals.Where(f => selectedGoalKey.Contains(f.Goal_Key));

    //Now returns 100 records
    return (from g in _Goals
            where selectedGoalKey.Contains(g.Goal_Key)
            select g);
}
3
  • 1
    What is the problem you're facing? Commented Oct 3, 2016 at 18:19
  • Why are you casting it to ObservableCollection<Goal>? That's your issue. Instead pass the IEnumerable to the ObservableCollection<Goal> constructor. Commented Oct 3, 2016 at 18:20
  • Both the Lambada and LINQ statements are returning null when it should be 100 records. Commented Oct 3, 2016 at 18:20

2 Answers 2

3

The problem is that the result is not an ObservableCollection<Goal> but an IEnumerable<Goal>. That is why you are receiving null.

You can do:

return new ObservableCollecion<Goal>
    (goals.Where(f => selectedGoalKey.Contains(f.Goal_Key)));

Using the "x" as "some type" casts the object to that type, and in the case it isn't able to returns null. What you want to do is to create a new ObservableCollecion and pass it the result of the linq query.

Out of MSDN:

The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception. Consider the following example:

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

Comments

0

Jmyster is not a good idea to work with ObservableCollection from the begining
Even with it inherits from Collection, it has much more boiler plates than you need while working on simple filters.(stuff like Notify events and more )

I´d strongly recommend you to make all your filltering using simple List, and only in the end of your algorithm put them all in a ObservableCollection class.
This simple behavior will prevent you from dealling with out-of-contexto problems.
Hope this help.

2 Comments

Well, I need to know when a record in goals is modified. Is there another way to do that besides Observable Collection?
You will still use it, but only in the end of your method.<br> Do all your logic using List. in the end convert it to ObservableCollection

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.