我計算從給定日期到當前日期的月份和年份,并且從這個串列中我必須減去在 sql (linq) 查詢中回傳給我的月份。我嘗試在結果上使用“Except”,但在下圖中給了我一個錯誤
var list = _ecpContext.Akceptacje_UnionAll_V
.Where(f => f.ADLogin == user)
.Select(f => new
{
Miesiac= f.Miesiac, //month
Rok= f.Rok // year
})
.ToList();
//-------------------------------------------------------------------
DateTime employmentDate = _ecpContext.Ustawienia.FirstOrDefault(x => x.UstLogin == user).EmploymentDate;
int employmentYear = employmentDate.Year;
int employmentMonth = employmentDate.Month;
DateTime now = DateTime.Now;
int currentYear = now.Year;
int currentMonth = now.Month;
var newList = Array.Empty<object>().Select(x => new { Month = 1, Year = 1 }).ToList();
for (var i = employmentYear; i <= currentYear; i )
{
for (var x = employmentMonth; x <= currentMonth; x )
{
newList.Add(new { Month = x, Year = i });
}
}
//-------------------------------------------------------------------
// i try
IEnumerable<DatesOfShortages> listMissingDates = list.Except(newList);
public class DatesOfShortages
{
public int Year { get; set; }
public int Month { get; set; }
}
新錯誤

uj5u.com熱心網友回復:
該Except方法是一種產生兩個序列的集差的方法,因此您需要呼叫它。
IEnumerable<DatesOfShortages> listMissingDates = newList.Except(list);
uj5u.com熱心網友回復:
你不能有一個充滿匿名型別的串列 A 和另一個充滿元組的串列 B,并對它們運行 a.Except(b)
列出匿名型別而不是元組:
var newList = Array.Empty<object>().Select(x => new { Month = 1, Year = 1 }).ToList();
for (var i = employmentYear; i <= currentYear; i )
{
for (var x = employmentMonth; x <= currentMonth; x )
{
newList.Add(new{ Month = x, Year = i});
}
}
因為newList我想類似的東西也new [] { list.ElementAtOrDefault(-1) }.ToList();可以作業..無論你想拉什么樣的技巧來獲取 ATs 串列!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/395567.html
