这里简单的linq to entity返回anonymous type
。问题是int?
值中的一个像参数一样使用另一个值int
。
我所知道的所有方法都不起作用。请告知如何解决这个问题。
public IQueryable<toursistdata> GetxTouristByCategory(string category)
{
var now = System.DateTime.MinValue;
int i;
switch (category)
{
case "arrival":
now = System.DateTime.Now.AddDays(-3);
var arrival = from a in db.xTourist
where a.ArrDate >= now && a.Room == null
select new toursistdata
{
kodt = a.Kod,
name = a.Name,
paxad = a.Paxad,
paxch = a.Paxch,
**//Here is h.Kod is int but KodH is int?**
hotel = (from h in db.Address where h.Kod = (int)a.KodH.HasValue select h.NameC).FirstOrDefault(),
room = a.Room,
arrdate = a.ArrDate,
arrtime = a.ArrTime,
arrflight = a.ArrFl,
depdate = a.DepDate,
deptime = a.Deptime,
depflight = a.DepFlight,
transfer = a.Transfer
};
return arrival;
case "inhouse":
now = System.DateTime.Today;
//return db.xTourist.AsQueryable().Where(p => p.Датапр >= now && p.Номер != null).OrderByDescending(p => p.Датапр);
default:
//return db.xTourist.AsQueryable();
}
}
答案 0 :(得分:6)
首先有一个&#39; =&#39;您的equals运算符中缺少。那么,如果你想比较int和int?你可以使用Null Coalescing运算符??为null-case 提供默认值,然后将其强制转换为int :
h.Kod == (a.KodH ?? 0)
答案 1 :(得分:2)
错误的行应该是
from h in db.Адреса where h.Kod = a.KodH.Value select h.NameC).FirstOrDefault()
from h in db.Адреса where h.Kod = (a.KodH ?? -1) select h.NameC).FirstOrDefault()
-OR -
from h in db.Адреса
where a.KodH!= null && h.Kod= a.KodH.Value
select h.NameC).FirstOrDefault()
有关使用可空类型的更多详细信息,请参阅MSDN Documentation