如何使用LINQ从函数返回值

时间:2011-07-05 09:48:58

标签: linq-to-sql

下面是代码示例我无法确定返回类型,此函数属于类clsExhibitorlist。我想将它绑定到我的gridview。

公众??? 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;
        }
    }

1 个答案:

答案 0 :(得分:2)

啊我在代码中看不到任何实际调用查询的内容。因此该方法返回表示查询的表达式树。

如果在return语句中添加类似.ToList()的内容,则会强制查询查询,然后返回类型

列表< clsExhibitorList>

您可以在此博文中找到对此处发生的事情的解释(延迟评估): http://devlicio.us/blogs/derik_whittaker/archive/2008/04/07/linq-and-delayed-execution.aspx

请注意,我不相信你可以绑定表达式树,你将添加.ToList或.ToArray或.ToDictionary或类似的,添加它的最佳位置将在return语句上,以便尽可能多的处理将在数据库中出现