This is a migrated thread and some comments may be shown as answers.

Return Type in LINQ

3 Answers 59 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
john
Top achievements
Rank 1
john asked on 27 Nov 2012, 05:21 AM
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?

3 Answers, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 27 Nov 2012, 02:46 PM
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.
0
john
Top achievements
Rank 1
answered on 27 Nov 2012, 03:01 PM
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?

0
Accepted
Doroteya
Telerik team
answered on 29 Nov 2012, 01:41 PM
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.
Tags
General Discussions
Asked by
john
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
john
Top achievements
Rank 1
Share this question
or