Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 这是在查询后设置更多属性值的好方法吗?_C#_.net Core - Fatal编程技术网

C# 这是在查询后设置更多属性值的好方法吗?

C# 这是在查询后设置更多属性值的好方法吗?,c#,.net-core,C#,.net Core,这就是我在查询后设置更多属性值的方法。请参阅方法GetMoreData(),它将对每个细节循环执行,并开始导致性能问题 有更好的方法吗?我知道在对象实例化期间执行的类构造函数。但在这种情况下,必须在数据库查询之后设置更多属性 注意:这是源代码的精简版本,只关注重要部分,如果有输入错误,请接受 public class OrderHeader { // Real database column public string OrderId { get; set; } publ

这就是我在查询后设置更多属性值的方法。请参阅方法GetMoreData(),它将对每个细节循环执行,并开始导致性能问题

有更好的方法吗?我知道在对象实例化期间执行的类构造函数。但在这种情况下,必须在数据库查询之后设置更多属性

注意:这是源代码的精简版本,只关注重要部分,如果有输入错误,请接受

public class OrderHeader
{
    // Real database column
    public string OrderId { get; set; }
    public string ColA { get; set; }
    public string ColB { get; set; }
    public string PostCode { get; set; }
    public decimal TotalWeight { get; set; }
    public decimal TotalShipmentFee { get; set; }
}

public class OrderDetail
{
    // Real database column
    public string OrderId { get; set; }
    public string OrderLine { get; set; }
    public decimal Weight { get; set; }
    public decimal ShipmentFee { get; set; }

    // mock-up column for displaly only, not exist in datababse
    public string ColA { get; set; } 
    public string ColB { get; set; } 
}
 
public static List<OrderDetail> GetByItemClass(string itemClass) 
{
    using (IDbConnection db = new MySqlConnection(connectionStringGoesHere)) {

        string sqlCmd = @"SELECT * FROM orderdetail WHERE ItemClass = @ItemClass";

        List<OrderDetail> orderDetails = db.Query<OrderDetail>(sqlCmd, new {
            @ItemClass = itemClass
        }).ToList();
        
        // Get or build or calculate additional properties
        for (int i = 0; i < orderDetails.Count; i++) {
            orderDetails[i] = GetMoreData(orderDetails);
        }

        return orderDetails;

    }
}

public static List<OrderDetail> GetByItemType(string itemType) 
{
    using (IDbConnection db = new MySqlConnection(connectionStringGoesHere)) {

        string sqlCmd = @"SELECT * FROM orderdetail WHERE ItemType = @ItemType";

        List<OrderDetail> orderDetails = db.Query<OrderDetail>(sqlCmd, new {
            @ItemType = itemType
        }).ToList();
        
        // Get or build or calculate additional properties
        for (int i = 0; i < orderDetails.Count; i++) {
            orderDetails[i] = GetMoreData(orderDetails);
        }

        return orderDetails;

    }
}

public static OrderDetail GetMoreData(OrderDetail orderDetail)
{

    // Performance problem: Need to query order header for every
    // single loop even multiple records having the same OrderId
    OrderHeader orderHeader = OrderHeaderDal.GetById(orderDetail.OrderId);
    
    // Directly map value
    orderDetail.ColA = orderHeader.ColA;
    orderDetail.ColB = orderHeader.ColB;
    
    // Calculate value
    if (orderHeader.PostCode == "0") {
        orderDetail.ShipmentFee = orderDetail.Weight * 1.15;
        
    // More complex to get value from another table
    } else {
        
        // Might also cause performance issue for many query loop
        Rate rate = RateDal.GetByPostCode(orderHeader.PostCode);

        orderDetail.ShipmentFee = orderDetail.Weight * rate.RatePerKg;
    }

    return orderDetail;
}
公共类OrderHeader
{
//实数据库列
公共字符串OrderId{get;set;}
公共字符串{get;set;}
公共字符串ColB{get;set;}
公共字符串邮政编码{get;set;}
公共十进制总权重{get;set;}
公共十进制TotalShipmentFee{get;set;}
}
公共类OrderDetail
{
//实数据库列
公共字符串OrderId{get;set;}
公共字符串顺序行{get;set;}
公共十进制权重{get;set;}
公共十进制ShipmentFee{get;set;}
//仅用于显示的实体模型列,不存在于数据库中
公共字符串{get;set;}
公共字符串ColB{get;set;}
}
公共静态列表GetByItemClass(字符串itemClass)
{
使用(IDbConnection db=new MySqlConnection(connectionstringgoesher)){
字符串sqlCmd=@“从orderdetail中选择*,其中ItemClass=@ItemClass”;
List orderDetails=db.Query(sqlCmd,新建{
@ItemClass=ItemClass
}).ToList();
//获取、构建或计算其他属性
for(int i=0;i
没有足够的细节来确定,但我认为您访问数据库的次数太多了。也许你可以让dapper做你的属性映射,在orderdetails和OrderHeader之间提供一个适当的连接查询。这被称为N+1问题。您需要执行一个大的联合更新,并使用Dapper multi mapping将其拆分。或者在同一批中执行两个查询,一个查询标题,一个查询详细信息,然后使用
QueryMultiple