[英]Convert nullable int to int in linq query return anonymous type
here simple linq to entities query that return anonymous type
. 这里简单的linq to实体查询返回
anonymous type
。 The problem is that one of the int?
问题是其中一个
int?
values used like parameter to get another value that is int
. 像参数一样使用的值来获取另一个
int
值。
All ways that I know is not working. 我知道的所有方法都不起作用。 Please advise how to solve this problem.
请告知如何解决这个问题。
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();
}
}
First there is one '=' missing in your equals operator. 首先,你的equals运算符中缺少一个'='。 Then, if You want to compare an int with an int?
那么,如果你想比较int和int? you can use the Null Coalescing operator ??
你可以使用Null Coalescing运算符?? to provide a default value for the null-case
, then cast it to an int : 为null-case提供默认值
,然后将其强制转换为int :
h.Kod == (a.KodH ?? 0)
The faulty line should read 错误的行应该读取
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()
For more details on working with nullable types refer to MSDN Documentation 有关使用可空类型的更多详细信息,请参阅MSDN文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.