如何解决使用动态 linq 将多对一展平
我有三个模型需要包含在报告中:
public partial class Chimney
{
public int Id { get; set;}
public string Name { get; set; }
public string Code { get; set; }
public int IndustrialUnitId { get; set; }
public IndustrialUnit IndustrialUnit { get; set; }
}
public partial class IndustrialUnit
{
public int Id { get; set;}
public string Code { get; set; }
public string UnitName { get; set; }
public string CompanyName { get; set; }
}
public partial class inspectionReport : BaseEntity
{
public string BusinessUnitName { get; set; }
public int UnitTypeId { get; set; }
public int? IndustrialUnitId { get; set; }
public IndustrialUnit IndustrialUnit { get; set; }
}
在我的 Web 应用程序的动态报告生成器中,用户选择实体,然后选择该实体的字段以创建报告,将向用户显示树节点结构。该树以嵌套形式显示实体及其父级和子级的字段。 例如,对于上述模型,如果用户选择 Chimney 获取报告,则 IndustrialUnit 及其子项(如 inspectionReport)将出现在树节点中。
之后,使用一个函数创建一个带有动态 Linq 的查询,它获取一个实体作为参数,并为该实体创建一个可查询的平面。
public static IQueryable ApplyFlattenProjection( Projection projection)
{
var entities = this.Context.GetDatas(entityType);
string select = string.Join(",",entities.ElementType.GetProperties().Where(x => !x.GetCustomAttributes(typeof(NotMappedAttribute)).Any()).Select(x => $"{x.Name} As {x.Name}"));
entities = entities.Select($"new(new({select}) As {projection.EntityType.Name})");
entities = entities.GetFlattenProjection(projection);
return entities;
}
internal static IQueryable GetFlattenProjection(this IQueryable queryable,Projection root)
{
.
.
.
var fieldName="IndutrialUnit.inspectionReports";
queryable = queryable.SelectMany($"{fieldName}.DefaultIfEmpty()",$"new(param2 as
{fieldName},{selector}param1.
{root.EntityType.Name} as {root.EntityType.Name})","param1","param2");
.
.
.
}
在这段代码中,selectMany 抛出异常,因为 IndustrialUnit 不是一个集合而是一个对象。如何处理这个问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。