简体   繁体   中英

Best way to migrate Datatable functions to Entity Framework

I am migrating a Windows Forms app to a web environment using Web Api / AngularJs / Entity Framework 6. This application has a reporting feature where basically the user picks from a set of reports and creates a query that filters data by date, ID range, and status. The method shown below builds the SELECT / WHERE statement and returns the results as a datatable that is bound to a datagridview, so the code is very generic and flexible. I would like to migrate this to EF and I'm wondering what would be the best approach... 1. Create interfaces and DTOs representing each select statement (body)? 2. how could I tag the where clause to a EF lambda expressions dinamically? Is this the best option?

I am open to ideas on how to approach this task in a elegant and efficient way.

   public DataTable GetReportData(ReportArgs rArgs)
    {
        string strSql = string.Empty;
        string whereClause = string.Format(" WHERE {0} {1}",GetDateRange(rArgs), GetIdRange(rArgs));
        string fechaI = rArgs.MatchesFechaI ? string.Format("'{0}'", rArgs.FechaI.ToShortDateString()) : "NULL";
        string fechaF = rArgs.MatchesFechaF == EnumCompararFecha.igual ? string.Format("'{0}'", rArgs.FechaF.ToShortDateString()) : "NULL";
        try
        {
            switch ((EnumReportes)rArgs.ReportNumber)
            {

                case EnumReports.GainTotal:
                    strSql = string.Format("SELECT Id,DateInitial as [Date Initial], WeightInitial as [Weight Initial], DateFinal AS [Date Final], Weightfinal as [Weight Final], Gainday as Gain FROM RepGainTotal {0} ", whereClause);
                    break;

                    ...

                    case EnumReports.GainByPeriod:
                    // In this case the Where clause is built inside of buildStringGainPeriodo(rArgs)
                    strSql = buildStringGainPeriodo(rArgs);
                    SetRepGainsPeriodo(rArgs, strSql);
                    strSql = strSql + " ORDER BY 2";
                    break;
                case EnumReports.Bulls:
                    strSql = string.Format("SELECT Id,Weight,Date1,Date2,Date3,LastGain,Total as [G. Total],round(WeightHoy,0) as [Weight Hoy]  FROM ANALISIS_CEBA {0} ", whereClause);
                    break;
                case EnumReports.SalesVsbuys:
                    strSql = string.Format("SELECT Id,Date_Sale as [Date Sale],Weight_Sale as [Weight Sale],Precio_kilo as [Precio Kilo]," +
                                           "Date_buy as [Date buy], Kilos_buy as [Weight (c)],cost_kilo as [cost Kilo], " +
                                           "GainDailyWeights as [G/day (Weights)],GainDailyGrams as [G/day (Grams)] " +
                                           " FROM SalesVsbuys {0} ", whereClause);
                    SetRepSalesVsbuys(rArgs, whereClause);
                    break;
                case EnumReports.SalesByPeriod:
                    strSql = string.Format("SELECT * FROM Sales {0} ", whereClause);
                    SetRepSales(rArgs, whereClause);
                    break;
                case EnumReports.BuysByPeriodo:
                    strSql = string.Format("SELECT * FROM buys {0} ", whereClause);
                    SetRepbuys(rArgs, whereClause);
                    break;
                case EnumReports.TasksByPeriod:
                    strSql = string.Format("SELECT Date, Task, TotalFROM RepMovimientos {0} ", whereClause);
                    SetRepMvtos(rArgs, whereClause);
                    break;
                case EnumReports.WeightsByPeriod:
                    strSql = string.Format("SELECT Date, Id, Weight FROM WeightHistory {0} order by Date,ID", whereClause);
                    SetRepPesajes(rArgs, whereClause);
                    break;
        }
    }
}       

I found a solution: using the EF SqlQuery method to load DTOs that represent views. SO I still get the flexibility of ad-hoc sql and I am able to then use linq, data binding etc :

''' string strSql = string.Format("SELECT InitialDate, etc FROM RepLifetimeGain"); string whereClause = WhereClauseBuilder.BuildWhereClause(rArgs);

using (GlContext gContext = new GlContext()) { var gvida = await gContext.RepLifetimeGain.SqlQuery(string.Format("{0} {1}", strSql,whereClause)).ToListAsync();

... }

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