Linq错误无法对匿名类型的变量进行操作

oxf4rvwz  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(101)

很显然我在星期三早上犯了一个愚蠢的错误。我有一个错误,我应该知道答案。对不起,愚蠢的问题。

var results1 = (from p in db1.a_PCRs
            join je in db1.a_Job_Extensions on p.PCR equals je.PCR
            join j in db1.Jobs on je.Job equals j.Job1
            join d in db1.Deliveries on j.Job1 equals d.Job
            where j.Status == "Active"
            where j.Job1.StartsWith("A")
            where p.PCR == "2552"
            where d.Shipped_Quantity > 0
            orderby d.Shipped_Date descending
            select new
            {
                p.PCR,
                d.Shipped_Date
            }).FirstOrDefault();

if (results1 != null)
{
    foreach (var result1 in results1)
    {
        lastDeliveryDate = result1.Shipped_Date;
    }

}else
{
    lastDeliveryDate = Convert.ToDateTime("1/1/1980");
}

错误发生在“results1”项目的foreach循环上。

Error   CS1579  foreach statement cannot operate on variables of type '<anonymous type: string PCR, DateTime? Shipped_Date>' because '<anonymous type: string PCR, DateTime? Shipped_Date>' does not contain a public instance or extension definition for 'GetEnumerator'

有谁能给我一个正确的方向吗?

hgb9j2n6

hgb9j2n61#

当调用.FirstOrDefault()时,您正在获取查询中的第一项。您不能用foreach迭代它,它是一个单独的项。
根据您的逻辑,您的查询已经获取了按'Shipped_Date'排序的正确项目。

if (results1 != null)
{
    lastDeliveryDate = results1.Shipped_Date;
}
else
{
    lastDeliveryDate = Convert.ToDateTime("1/1/1980");
}

相关问题