![](/img/trans.png)
[英]How to assign a nullable int property in an anonymous type in LINQ with a Union?
[英]Convert nullable int to int in linq query return anonymous type
这里简单的linq to实体查询返回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();
}
}
首先,你的equals运算符中缺少一个'='。 那么,如果你想比较int和int? 你可以使用Null Coalescing运算符?? 为null-case提供默认值,然后将其强制转换为int :
h.Kod == (a.KodH ?? 0)
错误的行应该读取
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()
-要么-
from h in db.Адреса where a.KodH!= null && h.Kod= a.KodH.Value select h.NameC).FirstOrDefault()
有关使用可空类型的更多详细信息,请参阅MSDN文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.