无法创建仅匿名类型的类型为匿名类型的常量值 [英] unable to create a constant value of type anonymous type only primitive types

查看:83
本文介绍了无法创建仅匿名类型的类型为匿名类型的常量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Entity Framework版本= 6.0.0.0来获取通用ID和订单ID,如下所示.

Using Entity Framework Version=6.0.0.0 to get to get common id and orderid as shown below.

var dt1 = from p in dt.AsEnumerable()
          select new
          {
              Id = p.Field<int>("Id"),
              OrderId = p.Field<int>("OrderId")
          };

var dt2 = (from order in db.Orders
           select new
           {
               order.Id,
               order.OrderId
           }).ToList();
var intersect = dt1.Intersect(dt2);

基于相交值列表.我需要从订单表"中选择所有值.

Based on the list of values in intersect. I need to select all the values from Orders Table.

试图使用代码获取错误无法创建仅匿名类型的常量类型的常量值"

Trying to used code getting error "unable to create a constant value of type anonymous type only primitive types"

var result= (from a in sync.Orders
              where intersect.Any(b => a.Id == b.Id && a.OrderId == b.OrderId)
              select a).ToList();

推荐答案

您正尝试将EF查询与内存中的数据集联接",该操作不起作用,因为无法嵌入列表和在SQL中查找.一种选择是使用AsEnumerable将整个表拉入内存:

You are trying to "join" an EF query with an in-memory data set, which does not work because there's not a way to embed the list and the lookup in SQL. One option is to pull the entire table into memory with AsEnumerable:

var result= (from a in sync.Orders.AsEnumberable
              where intersect.Any(b => a.Id == b.Id && a.OrderId == b.OrderId)
              select a).ToList();

另一种选择是将IdOrderId连接为一个值并使用Contains,因为可以将其转换为SQL中的IN子句:

Another option is to concatenate the Id and OrderId into one value and use Contains since that can be translated to an IN clause in SQL:

var lookup = intersect.Select(i => i.Id + "-" + i.OrderId).ToList();

var result= (from a in sync.Orders
              where lookup.Contains(a.Id + "-" + a.OrderId)
              select a).ToList();

这篇关于无法创建仅匿名类型的类型为匿名类型的常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆