[英]How to filter stored procedure result using linq
我有一个具有以下定义的存储过程:
Usp_totallisttype
select table1.name,table1.Id,table2.address,table1.type
from table1
inner join table2 on table1.Id = table2.Id
where (table1.type = 'A' OR table1.type = 'B')
order by table1.Id
CS:
public static List<classname> Getlist()
{
using (var Context = new DataContext())
{
var typelist = (from m in Context.Usp_totallisttype()
select new classname
{
name = m.name,
Id= m.Id,
address = m.address,
type = m.type
}).ToList();
if (typelist.Count > 0)
{
return typelist;
}
else
return null;
}
}
在这里,我正在调用List()方法:
List<classname> typeList = classname.List();
if (typeList != null)
{
var aList = typeList.where(p => p.type = "A").ToList();
//perform some opration and assign datasorce
var bList = typeList.where(p => p.type = "B").ToList();
//perform some opration and assign datasorce
}
问题:
我怎样才能做到这一点:
var aList = typeList.where(p => p.type = "A").ToList();//How i can do this ?
//perform some opration and assign datasorce
var bList = typeList.where(p => p.type = "B").ToList();//How i can do this ?
//perform some opration and assign data source
我是linq的新手,所以请让我知道上面的代码中缺少某些内容,或者如何进行优化。
我不清楚您将如何直接从对象p获取“ type”属性。 首先,您应该定义等于SQL查询结果结构的数据结构。
struct DBRecord
{
public string id
public string name {get;set;}
public string address {get;set;}
public string type {get;set;}
}
//Selection:
List<DBRecord> aList = typeList.FindAll(p => ((DBRecord)p).type == "A");
List<DBRecord> bList = typeList.FindAll(p => ((DBRecord)p).type == "B");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.