简体   繁体   中英

Join 2 tables in report viewer in ASP.NET MVC

I created report viewer in ASP.NET MVC, between 2 tables.

I created adapter to get all data in report like this :

在此处输入图片说明

But when I export report come like this, the expence_type does not show the data:

在此处输入图片说明

This is my code:

 public ActionResult Reports(string ReportType)
    {
        LocalReport localreport = new LocalReport();
        localreport.ReportPath = Server.MapPath("~/Reports/ExpenseReport.rdlc");



        ReportDataSource reportDataSource = new ReportDataSource();

        reportDataSource.Name = "DataSetE";

        var dataList = (from ed in _context.ExpenseDetails
         join e in _context.Expenses on ed.ExpensesId equals e.Expenses_Id

     select new { id = ed.ExpenseDetails_Id, amount = ed.Amount, expense = ed.DateExpense, eid = ed.ExpensesId, expenseType = e.Expenses_Type }).ToList();

        reportDataSource.Value = dataList;


        reportDataSource.Value = _context.ExpenseDetails.ToList();

        localreport.DataSources.Add(reportDataSource);

        string reportType = ReportType;
        string mimeType;
        string encoding;
        string fileNameExtension;

        if (reportType == "Excel")
        {
            fileNameExtension = "xlsx";
        }

        if (reportType == "Word")
        {
            fileNameExtension = "docx";
        }

        if (reportType == "PDF")
        {
            fileNameExtension = "pdf";
        }

        else
        {
            fileNameExtension = "jpg";

        }

        string[] streams;
        Warning[] warnings;
        byte[] renderedByte;

        renderedByte = localreport.Render(reportType, "", out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

        Response.AddHeader("content-disposition", "attachment;filename = expens_report." + fileNameExtension);

        return File(renderedByte, fileNameExtension);
    }

Expense Details Models :

public class ExpenseDetails
{
    [Key]
    public int ExpenseDetails_Id { get; set; }

    public double Amount { get; set; }

    public DateTime DateExpense { get; set; }

    public int ExpensesId { get; set; }

    [ForeignKey("ExpensesId")]
    public virtual Expenses expenses { get; set; }
}

Expenses Models :

   public class Expenses
{
    [Key]
    public int Expenses_Id { get; set; }
    public string Expenses_Type { get; set; }
}

How can I show the Expence_Type ?

reportDataSource.Value = _context.ExpenseDetails.ToList();

I tried to add the other table in this code but it is not returned.

在此处输入图片说明

在此处输入图片说明

You can create join between two table and assign this as your report data source

Try this

var dataList = (from ed in _context.ExpenseDetails
                 join e in _context.Expenses on ed.ExpensesId equals e.Expenses_Id 
                 select new {
                     id = ed.ExpenseDetails_Id ,
                     amount = ed.Amount ,
                     expense = ed.DateExpense ,
                     eid = ed.ExpensesId ,
                     expenseType = e.Expenses_Type 
                 }).ToList();

and add this

reportDataSource.Value = dataList ; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM