无法创建'匿名类型'的常量值

本文关键字:常量 类型 创建 | 更新日期: 2023-09-27 18:05:13

我有两个linq查询。我想在另一个查询中使用一个查询的结果。

var t2s = (from temp3 in _ent.Products                       
           where temp3.Row_Num == 2
           select new { temp3.ProductID });

然后我在另一个查询中使用这个变量:

var _query = (from P1 in _ent.brands
              join temp2 in on 
                  new { Produ_ID = (Int32?)P1.Prod_ID } 
                  equals new { Produ_ID = (Int32?)temp2.ProductID }
             );

当我单独运行第一个查询时,它会给我正确的结果。如果我在没有join的情况下运行第二个,它会给我正确的结果,但有join会给我以下错误:

错误:无法创建"匿名类型"的常量值。在此上下文中只支持基本类型('例如Int32, String和Guid')

无法创建'匿名类型'的常量值

您确定需要连接吗?

var t2s = _ent.Products.Where(t => t.Row_Num == 1).Select(t =>t.ProductID);
var _query = _ent.brands.Where(b => t2s.Contains(b.Prod_ID));

根据ProductID和/或Prod_ID是否为空,您可能需要稍微改变一些东西

尝试如下更改查询:

var t2s = from temp3 in _ent.Products                       
          where
             temp3.Row_Num == 2
          select temp3.ProductID;
var _query = from P1 in _ent.brands
             join temp2 in t2s on P1.Prod_ID equals temp2 
             select ...