0

我有一个具有以下定义的存储过程:

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 的新手,所以请让我知道上面的代码中缺少某些内容或如何使其更优化。

4

1 回答 1

1

我不清楚你将如何直接从对象 p 获取“类型”属性。首先,您应该定义数据结构等于 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");
于 2013-10-18T10:24:29.927 回答