Return Type in LINQ

4 posts, 1 answers
  1. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 26 Nov 2012 Link to this post

    I have two tables A and B. B is the sub table of A. Sometimes I want to create a method to return A.1, A.2, B.3, B.4 in LINQ. The key problem is how can I define the return type? Whether there are good solution for it?
  2. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 27 Nov 2012 Link to this post

    Hello John,

    The shape of the return type in LINQ could be defined in two ways, depending on whether you need a strongly-typed result:
        - By an anonymous type - if you are going to consume the result in the same method where you retrieve it, or
        - By a predefined class - if the result will be consumed by another method

    In the first case, all you have to do is construct a statement similar to this:
    using (EntityDiagrams dbContext = new EntityDiagrams())
    {
        var result = from a in dbContext.As
                     join b in dbContext.Bs on a.Id equals b.AId
                     // where
                     select new { a1 = a.Property1, a2 = a.Property2, b3 = b.Property2, b4 = b.Property3 };
    }

    In the second case, you need to define a separate result class in your project:
    public class Result
        {
            public string a1 { get; set; }
            public string a2 { get; set; }
            public string b3 { get; set; }
            public string b4 { get; set; }
        }

    and construct the statement like this:
    using (EntityDiagrams dbContext = new EntityDiagrams())
    {
        var result = from a in dbContext.As
                     join b in dbContext.Bs on a.Id equals b.AId
                     // where
                     select new Result { a1 = a.Property1, a2 = a.Property2, b3 = b.Property2, b4 = b.Property3 };
    }


    All the best,
    Doroteya
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  3. DevCraft banner
  4. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 27 Nov 2012 Link to this post

    Hi Doroteya,

    Thanks for you reply. If I need a method to bind to GridView, whether your second solution is more good for the case? If so, whether there is a way which no need to create a predefined class to bind to GridView?

  5. Answer
    Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 29 Nov 2012 Link to this post

    Hi John,

    Generally, this task can be solved with both of the approaches. The first one, where you use an anonymous type is applicable if you have to populate a lot of grids, and the second one is better if you have a small amount of grids.

    Let's look at the following example: there is a web page with a single grid in it and it needs to be populated with data from the Northwind database. If you decide to use an anonymous type, the task can be solved like this:
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.AttachDatasourceToGrid();
        }
     
        private object RetriveGridData()
        {
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                var myGridResult = (from a in dbContext.Products
                join b in dbContext.Categories on a.CategoryID equals b.CategoryID
                select new
                {
                      Name = a.ProductName,
                      UnitPrice = a.UnitPrice,
                      Category = b.CategoryName,
                      Description = b.Description
                }).ToList();
                return myGridResult;
            }
        }
     
        private void AttachDatasourceToGrid()
        {
            this.myGrid.DataSource = this.RetriveGridData();
            this.myGrid.DataBind();
        }
    }

    If you decide to use a predefined class, I suggest you the following:
    1) Add a new class file in your project and define the resultset:
    public class ProductResultSet
        {
            public string Name { get; set; }
            public string Category { get; set; }
            public string Description { get; set; }
            public decimal? UnitPrice { get; set; }
        }

    2) Modify the RetrieveGridData() method like this:
    private IList<ProductResultSet> RetriveGridData()
    {
           using (EntitiesModel dbContext = new EntitiesModel())
           {
                 IList<ProductResultSet> myGridResult = (from a in dbContext.Products
                        join b in dbContext.Categories on a.CategoryID equals b.CategoryID
                        select new ProductResultSet
                        {
                             Name = a.ProductName,
                             UnitPrice = a.UnitPrice,
                             Category = b.CategoryName,
                             Description = b.Description
                        }).ToList();
                    return myGridResult;
           }
     }

    I hope that helps. If you have additional questions, do not hesitate to get back to us.

    Kind regards,
    Doroteya
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
Back to Top