Solved

How do I pass an aggregate Linq query to my view

Posted on 2011-02-15
5
725 Views
Last Modified: 2012-06-21
Hi,
I am trying to create a view from two tables in my database. I am using EF4 and ASP.NET MVC 3. Now I query these 2 tables and need to return a view model which contains all columns from the Report table and the business name column from the Customers table. In my view I just want to add one model name. How do I go about it? The attached code of course only returns the report data. So how do I get the one column from the Customers table in the same model (or meta model?)?
public IEnumerable<Report> GetMyReports(int resellerid)
        {
            ReportsAndName repn = new ReportsAndName();
            var reports = from c in entities.Customers
                          from r in entities.Reports
                          where c.CustID == r.CustomerID &&
                          c.Reseller_id == resellerid
                          select r;// String.Format("{0} - {1}", r, c.Business_name);
            
            return reports;

Open in new window

0
Comment
Question by:nikomanek
  • 3
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34900814
Hi nikomanek;

Do you want the results of the query, reports, to be a collection of type ReportsAndName?

Fernando
0
 

Author Comment

by:nikomanek
ID: 34900881
Hi, sorry, the ReportsAndName is a leftover from one of my attempts to create a repository which contains Reports and Customers. Did not really work.

I have entities which are from a EF4 model generated from the Customer and Report table and I would like to have all reports and the business name from Customers in my result.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 34901187
Hi nikomanek;

You need to create a class with all the fields you want back from the query and then change the select clause of the query as shown in the code snippet and that should do it.

public IEnumerable<ReportAndCustomerName> GetMyReports(int resellerid)
{
    var reports = from c in entities.Customers
                  from r in entities.Reports
                  where c.CustID == r.CustomerID &&
                  c.Reseller_id == resellerid
                  select new ReportAndCustomerName
                  {
                      CustomerID = r.CustomerID,
                      //... All other fields from Reprts and Customers
                      BusinessName = c.BusinessName
                  };
    
    return reports;
}

// This class is used to get all the needed info from the query
public class ReportAndCustomerName
{
    // List all the fields from Reports table
    public int CustomerID { get; set; }
    // All the other fields from Reports here
    
    // The BusinessName From the cuistomer table
    public string BusinessName { get; set; }   
}

Open in new window


Fernando
0
 

Author Closing Comment

by:nikomanek
ID: 34902594
Perfect, thank you!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34902624
Not a problem, glad I was able to help.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now