我有一个存储过程,其定义如下:
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的新手,所以请在上面的代码中告诉我有什么遗漏或者我如何让它更优化。
答案 0 :(得分:1)
我不清楚你将如何直接从对象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");