【问题标题】:How to filter data using Linq in List<object>如何在 List<object> 中使用 Linq 过滤数据
【发布时间】:2013-10-17 15:36:07
【问题描述】:

我正在调用存储过程以使用 Linq 从数据库中获取数据。此存储过程使用多个表来使用连接返回结果:

public static List<classname> GetMediaTemp()
{
var medialist = (from m in Context.sp_Temp() select new classname
                                            {
                                               str_image = m.str_image,
                                                str_image_type = m.str_image_type,
                                                str_photodrawvideo = m.str_photodrawvideo,
                                            }).ToList();
if (medialist.Count > 0)
{
   return medialist
}
}

一切正常,但现在我必须像在调用端一样过滤此对象列表中的数据

List<classname> photoList = GetMediaTemp();//Here i want to filter list on the basis on str_photodrawvideo column.

问题:

我如何执行这个过滤器?

提前致谢。如需更多信息,请告诉我。

【问题讨论】:

  • 你为什么使用List&lt;object&gt;而不是List&lt;TypeOfMedialist&gt;?如果您绝对不能有一个强类型列表(例如,您需要将许多不同类型的对象放入一个列表中),也许objList.OfType&lt;TypeOfMedialist&gt;() 会有所帮助。

标签: c# sql linq


【解决方案1】:

你可以这样做

var objList = Context.sp_Temp().ToList();
var photoList = objList.Where(o=>o._int_previous == 1).ToList();

或者

您可以将object 转换为构建对象列表的类,如下所示

var photoList = 
   (from pht in objList 
     let x=>(sp_TempResult)pht
     where x._int_previous == 1 select pht).ToList();

【讨论】:

  • sp_Temp 返回的类型是什么?为什么需要对象列表?
  • 我得到了 {sp_TempResult} 类型。
  • @Sunny 使用var objList = Context.sp_Temp().ToList();
  • 是的。这是实现过滤器的正确方法,并且还将返回 "sp_TempResult" 类型指定为 "List",而不是使用对象类型。
  • @Damith 在我的问题中我使用的是相同的。
猜你喜欢
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-16
  • 2015-04-13
相关资源
最近更新 更多