我有一个包含这种结构的列表:
public class Factor
{
public DateTime? date { set; get; }
public string InnovoiceId { set; get; }
public string explain { set; get; }
public string factroyName { set; get; }
public string tax { set; get; }
public string transport { set; get; }
public Int64? bedehkar { set; get; }
public Int64? bestankar { set; get; }
public Int64? mande { set; get; }
}
List<Factor> factors = new List<Factor>();
当我尝试执行此查询时:
foreach (Factor temp in factorSortList)
{
temp.date = paymentInfo.Where(i => i.invoiceNo == temp.InnovoiceId).Select(i => i.payDate);
}
此处 factorSortList 是已排序的因子列表。和 paymentInfo 是一个列表,我想获取一些信息,并将它们放入我当前的列表 factorSortList 。 payDate 是一个数据类型的列那是 DateTime 。
我收到了这个错误:
Error 22 Cannot implicitly convert type 'System.Linq.IQueryable<System.DateTime?>' to 'System.DateTime?'
我
答案 0 :(得分:1)
即使在你的例子中不会发生,Enumerable.Where
也可能返回多个对象。您需要做的是使用Enumerable.First
,它只返回一个对象:
temp.date = paymentInfo.First(i => i.invoiceNo == temp.InnovoiceId).payDate);
答案 1 :(得分:0)
尝试First
方法:
temp.date = paymentInfo.Where(i => i.invoiceNo == temp.InnovoiceId)
.Select(i => i.payDate)
.First();
或FirstOrDefault
如果要进行空检查:
temp.date = paymentInfo.Where(i => i.invoiceNo == temp.InnovoiceId)
.Select(i => i.payDate)
.FirstOrDefault();
答案 2 :(得分:0)
选择返回IEnumerable
,因此您必须提取精确值;我使用First()
。请尝试
foreach (Factor temp in factorSortList)
{
temp.date = paymentInfo.Where(i => i.invoiceNo == temp.InnovoiceId).Select(i => i.payDate).First();
}
答案 3 :(得分:0)
使用以下代码更新您的代码:
temp.date = paymentInfo.Where(i => i.invoiceNo == temp.InnovoiceId).Select(i => i.payDate).First();