Search This Blog

Wednesday, January 12, 2011

Linq To Sql return from function as IQueryable

Following is the example illustrating how we can return IQueryable<T> from a method which has linq to SQL statements and bind returned data to a asp.net repeater control:

public IQueryable GetParentCategory()
    {
        return (context.CategoryMaster
           .Where(c => c.PARENT_CATEGORY_ID == 0)
           .Select(c => new { c.PARENT_CATEGORY_ID, c.CATEGORY_NAME, c.CATEGORY_ID })).AsQueryable();
    }

In above example we need only three fields (Projection) from data source.

We can bind repeater with following code:

rptParentCategory.DataSource = new Products().GetParentCategory();
rptParentCategory.DataBind();

Here 'Product' is a class in 'App_code' containing method 'GetParentCategory'.

1 comment:

Comments