尝试将ENUM添加到IQueryable

时间:2013-08-19 10:19:45

标签: c# asp.net-mvc linq

它出来空,没有数据,似乎仍然说匿名类型。编译确定并且只运行没有数据,感谢您到目前为止的所有帮助

public IQueryable<RentPaidReportRecord> rptRentPaid(string daysFrom, string daysTo, int IN_SiteId, int IN_LandownerId, int IN_PaymentCategoryId, int IN_PaymentTypeId, string IN_ShowRelevantProportion, string IN_OutputFormat,string @IN_URL)
{

    DateTime IN_daysFrom = Convert.ToDateTime(daysFrom);
    DateTime IN_daysTo = Convert.ToDateTime(daysTo);

    var result = this._lmsDb.rptRentPaid(IN_daysFrom, IN_daysTo, IN_SiteId, IN_LandownerId, IN_PaymentCategoryId, IN_PaymentTypeId, IN_ShowRelevantProportion, IN_OutputFormat,IN_URL);

    // Need to add in ENUM PaymentCategory to the Output then add col to report, 19/8/2013
    // So i've decared paymentCategoryValues below, now need to join it to the SearchResults below
    // i.e. SearchResults.PaymentCategoryId = paymentCategoryValues.PaymentCategoryId
    //... and be able to Reference the description as Category in the SearchResults object
    // Being carefull not to change the IQueryable since its working at the moment just need enum desc col
    var paymentCategoryValues =
        Enum.GetValues(typeof(PaymentCategory)).Cast<PaymentCategory>().Select
            (r => new KeyValuePair<int, string>((int)r, r.ToDescription()));

    var searchResults = (from s in result
                         from c in paymentCategoryValues
                         where (IN_SiteId <= 0 || s.SiteId == IN_SiteId)
                                && (IN_LandownerId <= 0 || s.LandownerId == IN_LandownerId)
                                && (IN_PaymentCategoryId <= 0 || s.PaymentCategoryId == IN_PaymentCategoryId)
                                && (IN_PaymentTypeId <= 0 || s.PaymentTypeId == IN_PaymentTypeId)
                                && (s.PaymentCategoryId == c.Key)
                         select new RentPaidReportRecord
                         {
                             SiteId = s.SiteId,
                             LandownerId = s.LandownerId,
                             PaymentCategoryId = s.PaymentCategoryId,
                             PaymentTypeId = s.PaymentTypeId,
                             Category = c.Value.ToString()
                         });


        return searchResults.AsQueryable();
}

public class RentPaidReportRecord
{
    public int SiteId { get; set; }
    public int LandownerId { get; set; }
    public int PaymentCategoryId { get; set; }
    public int PaymentTypeId { get; set; }
    public string Landowner { get; set; }
    public string SiteDescription { get; set; }
    public string RentalElection { get; set; }
    public string Period { get; set; }
    public System.Decimal Total { get; set; }
    public string Category { get; set; }
    public System.Decimal RelevantProportion { get; set; }
    public string ShowRelevantProportion { get; set; }
    public string URL { get; set; }
}

1 个答案:

答案 0 :(得分:0)

不太清楚函数的输出应该是什么,因为它不能是this._lmsDb.rptRentPaid返回的任何实例,因为需要追加附加字段(Category)。

因此,如果返回匿名/动态类型,则可以执行以下操作:

var searchResults = (from s in result
                     from c in paymentCategoryValues 
                     where (IN_SiteId <= 0 || s.SiteId == IN_SiteId)
                            && (IN_LandownerId <= 0 || s.LandownerId == IN_LandownerId)
                            && (IN_PaymentCategoryId <= 0 || s.PaymentCategoryId == IN_PaymentCategoryId)
                            && (IN_PaymentTypeId <= 0 || s.PaymentTypeId == IN_PaymentTypeId)
                            && (s.PaymentCategoryId == c.Key)
                         select new {
                             SiteId = s.SiteId,
                             LandownerId = s.LandownerId,
                             PaymentCategoryId = s.PaymentCategoryId,
                             PaymentTypeId = s.PaymentTypeId,
                             Category = c.Category
                         });

如果匿名类型不正常,则必须创建一个新类(例如RentPaidReportRecord),该类映射报表所需的字段并包含其他Category字段并返回{ {1}}来自此功能。

修改 你提到你仍然有问题,所以我在下面添加了一个完整的工作示例。适用于匿名类型和特定的“报告”类。如果您的结果集包含无法映射到其中一个枚举的类别ID,我还添加了一个替代方法。在这个例子中,所有3个都给出了相同的输出

如果您仍然得到一个空的结果集,我建议您检查您的IQueryable<RentPaidReportRecord>值是否与CategoryId枚举的(整数)值实际匹配。还要检查原始结果集是否已空。

PaymentCategory