【问题标题】:SubSonic 3.0.0.3 | SimpleRepository | Formula property/field亚音速 3.0.0.3 |简单存储库 |公式属性/字段
【发布时间】:2009-08-03 08:03:53
【问题描述】:

我需要在 SubSonic | 上添加公式属性/字段简单存储库 有人能告诉我怎么做吗?还是不可能?

br, 没有身体

【问题讨论】:

    标签: subsonic subsonic3


    【解决方案1】:

    只需将 [SubSonicIgnore] 添加到上面的 LineCost

    所以

            [SubSonicIgnore]
            public decimal LineCost
            {
                get { return Qty * Convert.ToDecimal(LineCost); }
            }
    

    这是在 LineCost 被映射到数据库时发生的。

    【讨论】:

    • 它有效!但是 Podge,是满足上述要求的正确方法还是只是一种解决方法?
    • 我不认为这是一种解决方法,我觉得这是最好的解决方案。原因是您有一个计算发生的地方,如果您在 SQL/Linq 中执行此操作,则您正在将数据检索与计算结合起来,每次检索 OrderLine 时都必须包含计算,忘记并且您有一个不错的小功能。
    【解决方案2】:

    为什么不在对象定义本身内进行计算?

    所以

        public class OrderLine
        {
            public int OrderId { get; set; }
            public int Qty { get; set; }
            public decimal ProductPrice { get; set; }
            public decimal LineCost
            {
                get { return Qty * Convert.ToDecimal(LineCost); }
            }
        }
    

    【讨论】:

    • 感谢 Podge,我会试试这个。这是正确的方法还是只是一种解决方法?
    • Failed: SqlException: Invalid column name 'LineCost' @Podge,记住我正在使用 SimpleRepository 并手工制作我的类/对象
    【解决方案3】:

    我只能看到使用匿名类型的方法,然后您必须将类型转换为订单线(它不是很好)

                    var x =from o in repo.All<OrderLine>()    
                        select new   
                        {
                            OrderId = o.OrderId,
                            ProductPrice = o.ProductPrice,
                            Qty = o.Qty,
                            LineCost = o.ProductPrice * o.Qty
                        };
    
    
                List<OrderLine> orders = null;
                foreach (var t in x)
                {
                    orders.Add(new OrderLine
                    {
                        LineCost = t.LineCost,
                        OrderId = t.OrderId,
                        ProductPrice = t.ProductPrice,
                        Qty = t.Qty
                    });
    
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多