0

出来是空的,没有数据,好像还是匿名类型。编译正常并且不运行任何数据,感谢您迄今为止的所有帮助

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; }
}
4

1 回答 1

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返回一个。IQueryable<RentPaidReportRecord>

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

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

public static class SomeCategories
{
    public enum PaymentCategory
    {
        Category1 = 1,
        Category2 = 2,
        Category3 = 3,
        Category4 = 4,
        Category5 = 5,
    }

    public static string ToDescription(this PaymentCategory category)
    {
        return category.ToString();
    }
}

public class SomeRentDb
{
    static SomeRentRecord[] _records = new[]
    {
        new SomeRentRecord() { SiteId = 1, LandOwnerId = 2, PaymentCategoryId = 4, PaymentTypeId = 3, PropertyName = "Propery 1" },
        new SomeRentRecord() { SiteId = 1, LandOwnerId = 4, PaymentCategoryId = 2, PaymentTypeId = 4, PropertyName = "Propery 2" },
        new SomeRentRecord() { SiteId = 2, LandOwnerId = 4, PaymentCategoryId = 3, PaymentTypeId = 5, PropertyName = "Propery 3" },
        new SomeRentRecord() { SiteId = 3, LandOwnerId = 5, PaymentCategoryId = 4, PaymentTypeId = 6, PropertyName = "Propery 4" },
        new SomeRentRecord() { SiteId = 4, LandOwnerId = 4, PaymentCategoryId = 1, PaymentTypeId = 7, PropertyName = "Propery 5" },
        new SomeRentRecord() { SiteId = 5, LandOwnerId = 5, PaymentCategoryId = 5, PaymentTypeId = 8, PropertyName = "Propery 6" },
    };

    public class SomeRentRecord
    {
        public int SiteId;
        public int LandOwnerId;
        public int PaymentCategoryId;
        public int PaymentTypeId;
        public string PropertyName;
    }

    public IQueryable<SomeRentRecord> All
    {
        get { return _records.AsQueryable(); }
    }

    public IQueryable<SomeRentRecord> RecordsWhere(Expression<Func<SomeRentRecord, bool>> expr)
    {
        return _records.AsQueryable().Where(expr);
    }
}

public class RentPaidReportRecord
{
    public int SiteId;
    public int LandOwnerId;
    public int PaymentCategoryId;
    public int PaymentTypeId;
    public string PropertyName;
    public string Category;
}

class Program
{
    static void Main(string[] args)
    {
        var db = new SomeRentDb();
        var result = db.All;

        var paymentCategoryValues =
                Enum.GetValues(typeof(SomeCategories.PaymentCategory)).Cast<SomeCategories.PaymentCategory>().Select
                    (r => new KeyValuePair<int, string>((int)r, r.ToDescription()));

        var paymentCategoryDictionary = Enum
            .GetValues(typeof(SomeCategories.PaymentCategory))
            .Cast<SomeCategories.PaymentCategory>()
            .ToDictionary(r => (int)r, r => r.ToDescription());

        var searchResults = from s in result
                            from c in paymentCategoryValues
                            where s.LandOwnerId == 4 && s.PaymentCategoryId == c.Key
                            select new RentPaidReportRecord()
                            {
                                SiteId = s.SiteId,
                                LandOwnerId = s.LandOwnerId,
                                PaymentCategoryId = s.PaymentCategoryId,
                                PaymentTypeId = s.PaymentTypeId,
                                PropertyName = s.PropertyName,
                                Category = c.Value
                            };
        var searchResults2 = from s in result
                             from c in paymentCategoryValues
                             where s.LandOwnerId == 4 && s.PaymentCategoryId == c.Key
                             select new 
                             {
                                 SiteId = s.SiteId,
                                 LandOwnerId = s.LandOwnerId,
                                 PaymentCategoryId = s.PaymentCategoryId,
                                 PaymentTypeId = s.PaymentTypeId,
                                 PropertyName = s.PropertyName,
                                 Category = c.Value
                             };
        var searchResults3 = from s in result
                             where s.LandOwnerId == 4
                             select new
                             {
                                 SiteId = s.SiteId,
                                 LandOwnerId = s.LandOwnerId,
                                 PaymentCategoryId = s.PaymentCategoryId,
                                 PaymentTypeId = s.PaymentTypeId,
                                 PropertyName = s.PropertyName,
                                 Category = paymentCategoryDictionary[s.PaymentCategoryId]
                             };
        foreach (var r in searchResults)
            Console.WriteLine("{0} - {1}", r.PropertyName, r.Category);
        foreach (var r in searchResults2)
            Console.WriteLine("{0} - {1}", r.PropertyName, r.Category);
        foreach (var r in searchResults3)
            Console.WriteLine("{0} - {1}", r.PropertyName, r.Category);
        Console.ReadLine();
    }
}
于 2013-08-19T15:01:51.513 回答