[英]Trying to add a ENUM to a IQueryable
它出來是空的,沒有數據,似乎還是說匿名類型。 編譯正常,不運行任何數據,謝謝您到目前為止的所有幫助
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; }
}
還不清楚函數的輸出是什么,因為它不能是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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.