【发布时间】:2017-02-11 18:31:37
【问题描述】:
我正在尝试填充我的 DataGrid
dgGoals.ItemsSource = GetGoals(new int[] { 1, 2, 3 });
这是从不同进程加载数据的 In Memory 对象
static ObservableCollection<Goal> goals = new ObservableCollection<Goal>();
我尝试使用此示例 Linq version of SQL "IN" statement,但 lambda 和 LINQ 语句在应该是 100 条记录时都返回 null。
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>;
}
编辑已修复,现在可以使用
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);
}
【问题讨论】:
-
您遇到了什么问题?
-
你为什么要把它投射到
ObservableCollection<Goal>?那是你的问题。而是将IEnumerable传递给ObservableCollection<Goal>构造函数。 -
当应为 100 条记录时,Lambada 和 LINQ 语句都返回 null。