所以示例程序:有沒有辦法避免依賴於表達式的linq投影重複?
class Cat
{
public bool IsMale { get; set;}
public int TailLength { get; set; }
public string Name { get; set; }
public decimal ClawAttackFrequency { get; set; }
public List<DateTime> FeedingTimes { get; set; }
}
static void Main(string[] args)
{
var cats = new List<Cat>();
var someData = cats
.Select(x => new
{
x.Name,
x.IsMale,
LatestFeedingTime = x.FeedingTimes.Max(y => (DateTime?)y)
})
.Select(x => new
{
x.Name,
x.IsMale,
x.LatestFeedingTime,
WasFedRecently = x.LatestFeedingTime.HasValue && x.LatestFeedingTime.Value >= DateTime.Today.AddDays(-1)
});
}
這僅僅是一個玩具的程序,做什麼,但它說明我的問題是,爲了避免重複我得反而讓兩個突出的LatestFeedingTime表達和複製一堆屬性。
的選擇將是:
var someData = cats
.Select(x => new
{
x.Name,
x.IsMale,
LatestFeedingTime = x.FeedingTimes.Max(y => (DateTime?)y),
WasFedRecently = (x.FeedingTimes.Max(y => (DateTime?)y)).HasValue && (x.FeedingTimes.Max(y => (DateTime?)y)).Value >= DateTime.Today.AddDays(-1)
});
但這複製一個棘手的表達式來代替。
有沒有什麼辦法在c#中獲得兩全其美的好處。就像一種聲明表達式的方法,然後在相同的投影中使用兩次?
我現在看到幾個答案後,我過分簡化了我的例子。真正的問題更多地與linq相關。我會接受其中一個答案,以防有人真的想知道這個問題,也許會提出一個更好的問題。 – alun