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

use*_*231 6 .net c# linq-to-entities entity-framework anonymous-types

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

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

然后我在另一个查询中使用此var:

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 }
             );
Run Code Online (Sandbox Code Playgroud)

当我自己运行第一个查询时,它给了我正确的结果.如果我运行第二个没有join它给我正确的结果,但有一个join给我以下错误:

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

Phi*_*hil 3

您确定需要加入吗?那这个呢:

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));
Run Code Online (Sandbox Code Playgroud)

您可能需要稍微更改一些内容,具体取决于 ProductID 和/或 Prod_ID 是否可为空。