2011-07-05 164 views
0

下面是代碼示例我無法確定返回類型,此函數屬於類clsExhibitorlist。我想將它綁定到我的gridview。如何從函數返回值與LINQ

public ??? GetExhibitorList(){

 using (DataClasses1DataContext context = new DataClasses1DataContext()) 
     { 

      var Exhibitors = from c in context.Companies 
          join b in context.Booths on c.CoID equals b.CoID 
          join bc in context.BoothCategories 
          on b.BoothID equals bc.BoothID 
          join sp in context.SubProductCategories 
          on bc.SubProdCatID equals sp.SubProdCatID 
          join p in context.ProductCategories on 
          sp.ProdCatID equals p.ProdCatID 
          orderby c.CoID 
          select new clsExhibitorList { c.CoID, c.CompanyName, b.FPCoName,p.ProdCatID,sp.SubProdCatID}; 



      if (Keyword != "") 
      { 
       Exhibitors = Exhibitors.Where(c => c.CompanyName.Contains(Keyword)); 
      } 
      if (ProdCatID != "") 
      { 
       Exhibitors = Exhibitors.Where(c => c.ProdCatID.Equals(ProdCatID.Split(','))); 
      } 
      if (SubProdCatID != "") 
      { 
       Exhibitors = Exhibitors.Where(c => c.SubProdCatID.Equals(SubProdCatID.Split(','))); 
      } 
      return Exhibitors; 
     } 
    } 

回答

2

啊我沒有看到在代碼中實際調用查詢任何東西。所以該方法返回表示查詢的表達式樹。

如果您在返回語句中加入類似.ToList()查詢將被強制evaulated,然後返回類型將是

名單< clsExhibitorList>

你可以找到一個交代什麼是這個博客帖子在這裏(延遲評價)事情: http://devlicio.us/blogs/derik_whittaker/archive/2008/04/07/linq-and-delayed-execution.aspx

請注意,我不beleive可以綁定對一個表達式樹,你將有添加.ToList或.ToArray或.ToDictionary或類似的,最好的添加它的地方是o n返回語句,以便在數據庫中儘可能多地進行處理