【问题标题】:How to store multiple rows in one variable? C# Entity Framework如何在一个变量中存储多行? C# 实体框架
【发布时间】:2021-12-22 16:30:08
【问题描述】:
public static List<TruckWithModel> GetAllTrucks()
{
    using (DAD_BaldipContext ctx = new DAD_BaldipContext())
    {
        var x = ctx.TruckFeatureAssociation
                   .Include(t => t.Truck)
                   .Include(tf => tf.Feature)
                   .Include(tm => tm.Truck.TruckModel)
                   .Select(it => new TruckWithModel()
                                 {
                                     Colour = it.Truck.Colour,
                                     Size = it.Truck.TruckModel.Size,
                                     RentalPrice = it.Truck.DailyRentalPrice,
                                     Status = it.Truck.Status,
                                     Model = it.Truck.TruckModel.Model,
                                     Rego = it.Truck.RegistrationNumber,
                                     Features = it.Feature.Description
                                 }) ;

        return (List<TruckWithModel>)x.ToList();
    }
}

此代码从相关表TruckFeatureAssociation、TruckFeature、IndividualTruck 和TruckModel 中检索各种属性值。

我遇到的问题是TruckFeatureAssociation 最多有5 个条目用于同一辆卡车,该表是IndividualTruck 和TruckFeature 之间的连接表,其中TruckFeature 是各种功能的表。

对于每个 TruckFeatureAssociation,创建一个不同的 TruckWithModel 对象,即如果有 3 个相关联的功能,每辆卡车在我调用此函数的数据网格中显示三行。

我想要它以便所有功能都可以存储在一个对象中。

所以在上面的输出中,我只想要一行,说警报系统,镀铬轮子。

【问题讨论】:

  • 在t-sql 中我们使用stuff 来合并2 个标识为相同的记录
  • 你能给我看一个这样的工作例子吗
  • 或添加到我当前解决方案的代码
  • 您应该从 Truck 实体而不是 TruckFeatureAssocation 开始查询,因为您希望每辆 Truck 有一个查询结果。然后,您可以添加一个用于将特征存储到返回类型的数组,或者如果您只想进行字符串连接操作,例如在网格中显示“feature1, feature2, feature3”。
  • 问题在于该功能在使用点符号的单个卡车上不可用

标签: c# sql-server entity-framework data-binding


【解决方案1】:

这里的问题是您正在查询特征,但模型反映的是卡车...查询卡车,获取它的特征,然后让您的视图模型 (TruckWithModel) 帮助为视图格式化该数据..

例如:

[Serializable]
public class TruckWithModel
{
    public string Colour { get; set; }
    public string Size { get; set; }
    public decimal RentalPrice { get; set; }
    public string Status { get; set; }
    public string Model { get; set; }
    public List<string> Features { get; set; } = new List<string>();

    public string FormattedFeatures
    {
       get { return string.Join(", ", Features); }
    }
}

现在查询数据时:

var trucks = ctx.Trucks
    .Select(t => new TruckWithModel()
    {
        Colour = t.Colour,
        Size = t.TruckModel.Size,
        RentalPrice = t.DailyRentalPrice,
        Status = t.Status,
        Model = t.TruckModel.Model,
        Rego = t.RegistrationNumber,
        Features = t.Features.Select(f => f.Description).ToList()
    }).ToList();    

这假设 Truck 有一个特征集合,其中 TruckFeatureAssociation 只是一个映射实体。如果您的卡车收藏基于 TruckFeatureAssociation:

        Features = t.Features.Select(f => f.Feature.Description).ToList()

现在,在您想要显示特征的视图中,绑定到 FormattedFeatures 属性以获取每辆卡车的特征列表(以逗号分隔)。

请注意,当您通过.Select() 使用投影时,您不需要使用.Include() 来预先加载相关实体。 EF 可以自动计算出加载什么来满足投影。

【讨论】:

    猜你喜欢
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多