LINQ的2实体:执行联接两列不同类型 [英] Linq 2 Entities : Performing a join on two columns with different types

查看:253
本文介绍了LINQ的2实体:执行联接两列不同类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用实体框架,我想用一个连接上的两列,键入字符串,之一是与其他类型的的Int32'的执行LINQ查询

I'm currently using Entity framework, and I want to perform a Linq query with a join on two columns, one being of type 'String', and the other of type 'Int32'.

Somethign类似于

Somethign similar to

from FirstEntity obj in context.FirstEntity 
                      join SecondEntity obj2 in context.SecondEntity on obj.SecondEntityId equals obj2.Id.ToString()

与obj.SecondEntityId为String和obj2.Id作为的Int32

with obj.SecondEntityId as String and obj2.Id as Int32

不幸的是,的ToString操作不会看到由EF支持。我无法摆脱这种错误(或类似的错误,如果我想转换成字符串的Int32):

Unfortunately, the ToString operation does not seen to be supported by EF. I get stuck with this error (or some similar errors, if I want to convert string to Int32):

LINQ到实体不能识别方法System.String的ToString()方法

和到现在为止,我没能找到周围的工作。

and until now, I was not able to find a work around.

THX的任何帮助。

推荐答案

我们一般把这样的事情成2查询(缺少的ToString()在LINQ to实体支持让我想害小孩)。

We typically break something like this up into 2 queries (lack of ToString() in linq to entities support makes me want to harm small children).

var query1 = (from SecondEntity obj2 in context.SecondEntity
             select obj2.ID).ToList();

// now we're using linq to objects which does support ToString()
query1 = query1.Select(x => x.ToString());

// mixing linq to entities and linq to objects
var query2 = from FirstEntity obj in context.FirstEneity
             join SecondEntity obj2 in query1 on obj.SecondEntityId equals obj2.ID

我这样做没有VS,所以一些语法可能是错误的,它不是一个特别好的解决办法,但EF的的V1。

这篇关于LINQ的2实体:执行联接两列不同类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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