クエリから を抽出してオブジェクトに割り当てる汎用関数を作成しようとしています。実際のlinq2sqlオブジェクトは、その中のプロパティを公開するために汎用関数で解決されていないようです。
どんな助けでも大歓迎です。
public Production GetData()
{
var productionData = new Production();
var query = (from o in _dbContext.ProductionDatas
select new Data<ProductionData>
{
td = _dbContext.ProductionDatas.Where(x => x.Timestamp <= _reportDate).ToList(),
mtd = _dbContext.ProductionDatas.Where(x => x.Timestamp >= _monthStartDate && x.Timestamp <= _reportDate).ToList(),
ytd = _dbContext.ProductionDatas.Where(x => x.Timestamp >= _yearStartDate && x.Timestamp <= _reportDate).ToList(),
today = _dbContext.ProductionDatas.FirstOrDefault(x => x.Timestamp == _reportDate),
}).FirstOrDefault();
AssignValue(productionData, query);
return productionData;
}
public Production GetDiscardedData()
{
var productionData = new Production();
var query = (from o in _dbContext.ProductionDiscardedDatas
select new Data<ProductionData>
{
td = _dbContext.ProductionDiscardedDatas.Where(x => x.Timestamp <= _reportDate).ToList(),
mtd = _dbContext.ProductionDiscardedDatas.Where(x => x.Timestamp >= _monthStartDate && x.Timestamp <= _reportDate).ToList(),
ytd = _dbContext.ProductionDiscardedDatas.Where(x => x.Timestamp >= _yearStartDate && x.Timestamp <= _reportDate).ToList(),
today = _dbContext.ProductionDiscardedDatas.FirstOrDefault(x => x.Timestamp == _reportDate),
}).FirstOrDefault();
AssignValue(productionData, query);
return productionData;
}
private static void AssignValue<T>(Production productionData, Data<T> query) where T : class
{
if (query != null)
{
productionData.Today.Spec1 = query.today.Spec1;
productionData.Today.Spec2 = query.today.Spec2;
productionData.Today.Spec3 = query.today.Spec3;
productionData.MTD.Spec1 = query.mtd.Sum(x => x.Spec1 );
productionData.MTD.Spec2 = query.mtd.Sum(x => x.Spec2 );
productionData.MTD.Spec3 = query.mtd.Sum(x => x.Spec3 );
productionData.YTD.Spec1 = query.ytd.Sum(x => x.Spec1 );
productionData.YTD.Spec2 = query.ytd.Sum(x => x.Spec2 );
productionData.YTD.Spec3 = query.ytd.Sum(x => x.Spec3 );
productionData.TD.Spec1 = query.td.Sum(x => x.Spec1 );
productionData.TD.Spec2 = query.td.Sum(x => x.Spec2 );
productionData.TD.Spec3 = query.td.Sum(x => x.Spec3 );
}
}
public class Data<T>
{
public List<T> mtd;
public List<T> ytd;
public T today;
public List<T> td;
}