简体   繁体   中英

Entity Framework and Report Querying

What is the preferred/best way to query the database using Entity Framework and SqlQuery() where the return data is SUM and GROUP BY with various pieces of data from joined tables? I don't understand how to use existing POCO's to return a POCO that will represent the data queried.

For example, I have a Commission class:

public class Commission
{
    public long CommissionId { get; set; }
    public int OrderId { get; set; }
    public int OfferId { get; set; }
    public decimal TotalRevenue { get; set; }
    public int Quantity { get; set; }
    public int MerchantId { get; set; }
    public decimal MerchantRevenue { get; set; }
    public string MerchantRepresentativeId { get; set; }
    public decimal MerchantRepCommissionPercent { get; set; }
    public decimal MerchantRepCommissionAmount { get; set; }
    public DateTime DateCreated { get; set; }

    [NotMapped]
    public Offer Offer { get; set; }
    [NotMapped]
    public Merchant Merchant { get; set; }
    [NotMapped]
    public ApplicationUser MerchantRep { get; set; }

    public Commission()
    {
        this.DateCreated = DateTime.Now;
    }
}

I have a GetOfferCommissionListForMerchant() method like so:

    public static List<Commission> GetOfferCommissionListForMerchant(int merchantId, DateTime? dateFrom = null, DateTime? dateTo = null)
    {
        using (AppDbContext db = new AppDbContext())
        {
            if (merchantId > 0)
            {
                var comms = db.Commissions.SqlQuery("SELECT [OfferId],[MerchantId],[MerchantRepresentativeId],SUM([TotalRevenue]) AS TotalRevenue,SUM([Quantity]) AS Quantity,SUM([MerchantRevenue]) AS MerchantRevenue,SUM([MerchantRepCommissionAmount]) AS MerchantRepCommissionAmount FROM [Commissions] WHERE [DateCreated] BETWEEN @p0 AND @p1 GROUP BY [MerchantRepresentativeId], [OfferId],[MerchantId]", dateFrom, dateTo);

                return new List<Commission>();  **//This line is here to get the project to compile**
            }
            else
            {
                return new List<Commission>();
            }
        }
    }

Although this method returns only Commissions, I would like to get Merchant name (based off of MerchantId) which is in a different table and other related bits of data for each Commission row returned. I am just at a loss on how to accomplish this task.

I understand that this does not explain the whole domain I am working with but I am trying to understand the proper way to accomplish a task like this. I will have other reports that use similar data in different ways.

Do I need to revert to ADO.NET DataTable and DataSet methodologies? I was hoping that I could do all I used to do in the old ADO.NET world in this new POCO/Entity Framework world.

Your thoughts? Thanks!

you can but should not use Commission to report, Commission is an Entity, that is a type known by the context and binded to a table.

You should declare a specific type or use an anonymous one (but in this case it must be used in the function)

    class ReportType {
        MerchantRepresentativeId,
        TotalRevenu
    }

    /* ... */

    using (AppDbContext db = new AppDbContext())
    {
        if (merchantId > 0)
        {
            var comms = 
                from com in db.Commissions
                group com by com.MerchantRepresentativeId into comGroup
                select new ReportType {
                    MerchantRepresentativeId = com.MerchantRepresentativeId,
                    TotalRevenu = comGroup.Sum(x => x.TotalRevenu)                    
                }

            return comms.ToList();  **//This line is here to get the project to compile**
        }
        else
        {
            return new List<ReportType>();
        }
    }

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