0

テーブルをピボットに変換する次のコードがあります。

IQueryable<MeterReadingsForChart> meterReadings = MeterReadingManager.GetCustomerMeterReadings(customer.sno, MeterType, Meter, startDate, endDate, DateTimeManager.GetTimeIntervalTypeById(DateRangeType)).AsQueryable();

// meterReadings Output:
// Customer  Value ReadingTypes
// cust1       235           T1
// cust1       241           T2
// cust1       765           T3
// cust1       247           T4
// cust1       967        Total
// ..........

var table = MeterReadingManager.GetMeterReadingsPivot(meterReadings, MeterType);

// table Output (pivot of meterReadings):
// Customer     T1     T2    T3    T4    Total
// cust1       235    241   765   247      967
// ............

私はReadingTypesの文字列配列を持っています:

string[] selectedValues = Parameters.Split(','); 
// For example output : { "T1", "Total" }

ピボット列を動的に取得したい。上記の配列を使用します。私の予想されるピボットテーブル:

// filtered pivot from meterReadings pivot Output:
// Customer     T1    Total
// cust1       235      967
// ............

これどうやってするの?動的に選択しますか、それとも別の方法ですか?

また、変換関数は次のとおりです。

return (from m in meterReadings
        group m by new { date = m.ReadDate } into g
        select new
        {
           ReadDate = g.Key.date.ToString("dd.MM.yyyy - HH:mm:ss"),
           T1 = (from t1 in g
                where t1.Name == "T1"
                select t1.Value.ToString("0.00 " + t1.Unit + "")).FirstOrDefault(),
           T2 = (from t2 in g
                where t2.Name == "T2"
                select t2.Value.ToString("0.00 " + t2.Unit + "")).FirstOrDefault(),
           T3 = (from t3 in g
                where t3.Name == "T3"
                select t3.Value.ToString("0.00 " + t3.Unit + "")).FirstOrDefault(),
           Total_T1 = (from t1 in g
                      where t1.Name == "Toplam T1"
                      select t1.Value.ToString("0.00 " + t1.Unit + "")).FirstOrDefault(),
           Total_T2 = (from t1 in g
                       where t1.Name == "Toplam T2"
                       select t1.Value.ToString("0.00 " + t1.Unit + "")).FirstOrDefault(),
           Total_T3 = (from t1 in g
                      where t1.Name == "Toplam T3"
                      select t1.Value.ToString("0.00 " + t1.Unit + "")).FirstOrDefault(),
           Reactive = (from t1 in g
                      where t1.Name == "Reaktif"
                      select t1.Value.ToString("0.00 " + t1.Unit + "")).FirstOrDefault(),
           Capasitive = (from t2 in g
                        where t2.Name == "Kapasitif"
                        select t2.Value.ToString("0.00 " + t2.Unit + "")).FirstOrDefault()
           }).AsQueryable<object>();

ありがとう...

4

1 に答える 1

0

私はそれを解決しました:

var table = MeterReadingManager.GetMeterReadingsPivot(meterReadings, MeterType).Select("new (" + Parameters + ")");
于 2013-01-14T14:03:31.043 に答える