如何从另一种方法调用linq?

时间:2017-04-30 19:26:38

标签: c# .net linq visual-studio-2010

在这个方法中我创建了linq。 Linq工作正常,我可以正确地看到数据,但问题是我需要能够从另一个方法调用它。

  protected IQueryable method ()
    {

        var v = (from a in BAS1
                 join b in BAS2 on a.TIP equals b.TIP 
                 join c in BAS3 on a.COM equals c.COM 
                 join d in BAS4 on c.PROV equals d.PROV 
                 join e in BAS5 on d.ID_REG equals e.ID_REG

                 select new
                 {
                     a.ID,
                     b.SIG,
                     e.ID_REG,

                 });

        return v.ToString();
    }

从这个方法我想拿linq并过滤它,但它在 v = v.Where(x => ....

中显示错误
 public ActionResult method2() {

        var v = method ();
        var id = Request.QueryString["id"];
        var filter_sig = Request.QueryString["filter_sig"];
        var filter_reg = Request.QueryString["filter_reg"];
if (filter_sig!= "")
        {
            decimal dfiltesig = decimal.Parse(filter_sig);
            v = v.Where(x => x.SIG==dfiltesig );
        }

if (id!= "")
        {
            v = v.Where(x => x.ID.ToLower().Contains(id.ToLower()));
        }

if (filter_reg!= "")
        {
            v = v.Where(x => x.ID_REG.ToLower().Contains(filter_reg.ToLower()));
        }}

2 个答案:

答案 0 :(得分:1)

从您的return语句ToString()中删除return v.ToString();调用,而只返回查询

return v;

答案 1 :(得分:0)

public class MyThing {
   public string ID {get;set;}
   public string ID_REG {get;set;}
   public decimal SIG {get;set;}
}

protected IQueryable<MyThing> method()
{
    return (from a in BAS1
             join b in BAS2 on a.TIP equals b.TIP 
             join c in BAS3 on a.COM equals c.COM 
             join d in BAS4 on c.PROV equals d.PROV 
             join e in BAS5 on d.ID_REG equals e.ID_REG

             select new MyThing
             {
                 a.ID,
                 b.SIG,
                 e.ID_REG,
             });
}