Linq2Sql:无法理解是什么问题

时间:2013-06-27 05:29:47

标签: c# .net linq-to-sql

我正在尝试创建一个通用函数来从查询中提取并将其分配给对象。似乎实际的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;
}

1 个答案:

答案 0 :(得分:1)

您的AssignValue<T>方法实际上并非通用 - 它特定于ProductionData。 IMO您应该从此方法中移除<T>并将所有剩余的T直接替换为ProductionData


评论:

  

但是我有一些其他具有类似属性名称的L2S类,这也是我想要分配值的原因之一。例如。我有一个类 - ProductionData,ProductionDataRenewed,ProductionDataDiscarded等。所有这些L2S类都有3个类似的属性Spec1,Spec2,Spec3。

然后这里的另一个选择是“接口和约束”;例如:

interface ISpec {
    decimal Spec1 { get; }
    decimal Spec2 { get; }
    decimal Spec3 { get; }
}

使用:

public class Data<T> where T : ISpec {...}

并将: ISpec添加到ProductionData。如果ProductionData是生成的文件,不会编辑生成的文件 - 而是添加单独的代码文件:

namespace The.Correct.Namespace {
    partial class ProductionData : ISpec {}
}

组合(由编译器)与生成的文件,使用隐式接口实现来获取接口成员。

并将Assign<T>约束从where T : class更改为where T : class, ISpec应该可以。显然,如果您的数据不是decimal,则相应地更改ISpec