linq where包含在IEnumerable中

时间:2012-12-28 08:53:53

标签: c# linq ienumerable contains

//\\ ---- selected items from ChechBoxList
IEnumerable<int> selSender = (from ListItem item in CheckBoxList1.Items.OfType<ListItem>()
                                       where item.Selected
                                       select Convert.ToInt32(item.Value));

//\\ --- linq query
var messages = from m in myEntities.Messages
select new {
m.Id,
m.MessageText,
m.CreatedByUserID
};

我的问题是:我需要通过CheckBoxList“selSender”中的选定项目来过滤数据 我尝试了以下方法:

 RadGrid1.DataSource = messages.Where(m => selSender.Contains(m.CreatedByUserID));

但它没有用,错误信息:

实例参数:无法从'System.Collections.Generic.IEnumerable'转换为'System.Linq.IQueryable

如何根据selSender id获取数据?

1 个答案:

答案 0 :(得分:0)

看起来属性CreatedByUserID的类型为Nullable<int>。您需要使用它的值来验证selSender是否包含它:

RadGrid1.DataSource = from m in myEntities.Messages                          
                      where m.CreatedByUserID.HasValue &&
                            selSender.Contains(m.CreatedByUserID.Value)
                      select new { 
                          m.Id,
                          m.MessageText,
                          m.CreatedByUserID 
                      };