Sneha
Sneha

Reputation: 1

How do I return values from a function with LINQ

Below is the code sample I cant figure the return type, this function belongs to a class clsExhibitorlist. I want to bind this to my gridview.

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;
        }
    }

Upvotes: 0

Views: 44

Answers (1)

GreyCloud
GreyCloud

Reputation: 3100

Ah I dont see anything in the code to actually invoke the query. So the method returns an expression tree representing the query.

If you add something like .ToList() on the return statement the query will be forced to be evaulated and the return type will then be

List < clsExhibitorList >

You can find an explaination of what is going on here (Delayed Evaluation) in this blog post: http://devlicio.us/blogs/derik_whittaker/archive/2008/04/07/linq-and-delayed-execution.aspx

Note that i dont beleive you can bind against an expression tree to you will have add the .ToList or .ToArray or .ToDictionary or similar, best place to add it would be on the return statement so that as much processing as possible will occur in the database

Upvotes: 2

Related Questions