0
  1. 我正在创建一个表达式构建器或查询构建器来查询一个特定的实体。该实体称为 RequestBase,该实体具有一些属性,但还有 3 个其他实体继承自 Request Base。

  2. 因为我使用的是 TPH,所以只创建了一个带有鉴别器列的表,而所有其他属性都在 RequestBase 表中创建。

  3. 问题是当使用动态 LINQ 查询不在 RequestBase 实体中而是在子实体中的属性时。

**请求库代码**

public class RequestBase
    {
        public int RequestBaseId { get; set; }
        public string CurrentStatus { get; set; }
        public string RequestNumber { get; set; }
        public DateTime RequestDate { get; set; }
        public bool IsOnHold { get; set; }

        public virtual Dealer Dealer { get; set; }
        public virtual Requester Requester { get; set; }
        public virtual Vehicle Vehicle { get; set; }

        public virtual ICollection<Attachment> Attachments { get; set; }
        public virtual ICollection<WorkflowHistory> WorkflowHistories { get; set; }

}

** EcoBonusRequest 的代码**

public class EcoBonusRequest : RequestBase
    {
        public string BrandReturnedVehicle { get; set; }

        public string TypeReturnedVehicle { get; set; }

        public string ChassisReturnedVehicle { get; set; }

        public string DestructionCertificateNumberReturnedVehicle { get; set; }

        public decimal AmmountWithVAT { get; set; }

        public decimal AmmountWithoutVAT { get; set; }

        public decimal Percentage { get; set; }

        public DateTime VehicleDesctructionDate { get; set; }

        public virtual Customer Customer { get; set; }

        public string Comments { get; set; }
    }

**查询构建器的代码**

private string BuildQuery()
            {
                var sb = new StringBuilder();
                //var list = RequestBaseBL.GetRequestByCustomQuery("RequestNumber == \"12\"");

                #region 1st ROW of the QUERY
                if (ColumnType(DdlColumn1.SelectedValue) == "Text")
                {
                    if(DdlOperator1.SelectedValue == "==")
                    {
                        sb.Append(DdlColumn1.SelectedValue);
                        sb.Append(DdlOperator1.SelectedValue);
                        sb.Append("\"" + TxtValue1.Text + "\"");
                    }
                    if (DdlOperator1.SelectedValue == "<>")
                    {
                        sb.Append(DdlColumn1.SelectedValue);
                        sb.Append(DdlOperator1.SelectedValue);
                        sb.Append("\"" + TxtValue1.Text + "\"");
                    }
                    if (DdlOperator1.SelectedValue == "LIKE")
                    {
                        sb.Append(string.Format("{0}.Contains(\"{1}\")", DdlColumn1.SelectedValue, TxtValue1.Text));
                    }
                    if (DdlOperator1.SelectedValue == "NOT LIKE")
                    {
                        sb.Append(string.Format("!{0}.Contains(\"{1}\")", DdlColumn1.SelectedValue, TxtValue1.Text));
                    }
                }

                if (ColumnType(DdlColumn1.SelectedValue) == "Number")
                {
                    sb.Append(DdlColumn1.SelectedValue);
                    sb.Append(DdlOperator1.SelectedValue);
                    sb.Append(TxtValue1.Text);
                }

                if (ColumnType(DdlColumn1.SelectedValue) == "Date")
                {
                    sb.Append(DdlColumn1.SelectedValue);
                    sb.Append(DdlOperator1.SelectedValue);
                    sb.Append(TxtValue1.Text);
                }

                if (ColumnType(DdlColumn1.SelectedValue) == "Bool")
                {
                    sb.Append(DdlColumn1.SelectedValue);
                    sb.Append(DdlOperator1.SelectedValue);
                    sb.Append(TxtValue1.Text);
                }

                #endregion  


                return sb.ToString();
            }

        #endregion

**生成的应该执行的 sql **

public IQueryable<RequestBase> GetRequestByCustomQuery(string strql)
            {
                return _context.RequestBases.Where(strql);
            }

? sb.ToString()
"AmmountWithVat=1"

**错误**

“RequestBase”类型中不存在属性或字段“AmmountWithVat”

4

1 回答 1

2

以下将起作用:

    public IQueryable<RequestBase> GetRequestByCustomQuery(string strql)
    {
         return _context
               .RequestBases
               .OfType<EcoBonusRequest>()   //
               .Where(strql);
    }

您可以轻松调整此功能:

    public IQueryable<T> GetRequestByCustomQuery<T>(string strql)
        where T :  RequestBase
    {
         return _context
               .RequestBases
               .OfType<T>()   //
               .Where(strql);
    }

所以问题是如何在 BuildQuery() 中获取该类型。不容易,但这就是你设置它的方式。

于 2012-05-31T12:04:21.097 回答