c# - LINQ - 外键数据和表达式树

标签 c# asp.net-mvc linq

第一次发帖。如果我错过了什么,我深表歉意。我有一个 .NET MVC 程序,它接受来自用户的任意数量的过滤器,然后尝试根据这些过滤器从模型中检索数据。我在 Dynamic LINQ 和 Expression Trees 方面取得了成功,但只是在评估我的父模型(保险)上的字段时。我似乎无法弄清楚如何通过通过外键关系链接到 Insured 模型的 LINQ 正确访问子模型(Quote)。我的数据库模型:

    public class Insured {
        [Key]
        [Column(Order = 0)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long INS_CLIENT_ID { get; set; }

        [Key]
        [Column(Order = 1)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public string INS_AGENT_ID { get; set; }

        [Key]
        [Column(Order = 2)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long INS_UNQ_ID { get; set; }

        [Column("INS_NAME")]
        public string INS_NAME { get; set; }

        [ForeignKey("QTE_CLIENT_ID, QTE_AGENT_ID, QTE_INS_UNQ_ID")]
        public virtual ICollection<Quote> TBL_QTE { get; set; }
    }

    public class Quote {
        [Key]
        [Column(Order = 0)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long QTE_CLIENT_ID { get; set; }

        [Key]
        [Column(Order = 1)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public string QTE_AGENT_ID { get; set; }

        [Key]
        [Column(Order = 2)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long QTE_INS_UNQ_ID { get; set; }

        [Key]
        [Column(Order = 3)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public long QTE_NUMBER { get; set; }

        [Column("QTE_LOB")]
        public string QTE_LOB { get; set; }

        [Column("QTE_CO_CODE")]
        public string QTE_CO_CODE { get; set; }

    }

然后我的 Controller 文件中有一个 LINQ 填充 View 模型:

m.Insureds = (from i in repository.Insureds
              orderby i.INS_NAME
              select i).Where(GetWhere(s));

我的 GetWhere 函数看起来像这样(它接受不同的搜索选项 View 模型):

private Expression<Func<Insured, bool>> GetWhere(SearchOptions s) {
   ParameterExpression pe = Expression.Parameter(typeof(Insured));
   Expression left = Expression.Call(Expression.PropertyOrField(pe, "INS_AGENT_ID"), "ToUpper", null);
   Expression right = Expression.Constant(s.AgentId.ToUpper());
   Expression e1 = Expression.Equal(left, right);

   return Expression.Lambda<Func<Insured, bool>>(e1, pe);
}

一切正常。我只是无法将其扩展为能够查询 TBL_QTE 字段。我试过了……

m.Insureds = (from i in repository.Insureds
              where i.TBL_QTE.AsQueryable<Quote>().All<Quote>(GetLob(s))
              orderby i.INS_NAME
              select i);

GetLob 在哪里...

private Expression<Func<Quote, bool>> GetLob(SearchOptions s) {
    ParameterExpression pe = Expression.Parameter(typeof(Quote));
    Expression left = Expression.Call(Expression.PropertyOrField(pe, "QTE_LOB"), "ToUpper", null);
    Expression right = Expression.Constant(s.LOBCode.ToUpper());
    Expression e1 = Expression.Equal(left, right);

    return Expression.Lambda<Func<Quote, bool>>(e1, pe);
}

...但这也不完全正确,因为 Where 期望的是 Expression.Lambda 结果,而不是对其的调用。我只是不知道如何通过表达式树模拟到 TBL_QTE 属性的导航。我的直觉告诉我,我接近这一行,但还不够,因为它会产生 1025 数据提供程序错误:

i.TBL_QTE.AsQueryable<Quote>().All<Quote>(GetLob(s))

最佳答案

看起来你可能需要使用 c# linq 支持的“let”关键字,

这是一个例子(来自 LinqPad)

from p in Products let spanishOrders = p.OrderDetails .Where (o => o.Order.ShipCountry == "Spain") where spanishOrders.Any() orderby p.ProductName select new {
p.ProductName,
TotalValue = spanishOrders.Sum (o > => o.UnitPrice * o.Quantity) }

关于c# - LINQ - 外键数据和表达式树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23707269/

相关文章:

c# - 反序列化 SOAP XML 响应

c# - 为什么 PCL NuGet 包无法定位 Xamarin 平台?

c# - TKey 上具有不同数据类型的 OrderBy keySelector

asp.net-mvc - Entity Framework 代码优先多对多 NullReference

c# - 使用 ASP.NET Web API 并行进行基本身份验证和表单例份验证

c# - 日期数组,检查日期是否存在

c# - 使用 join、group-by 和 where 将 sql 转换为 linq

c# - 如何在 WP7 上动态填充网格?

c# - LinqToXML 对多个子层进行适当的空检查

c# - 非 Controller 类无法访问 DbContext