将两个EF查询,无法转换类型System.Data.Entity.Infrastructure.DbQuery的对象System.Collections.Generic.IEnumerable [英] Combine two EF Queries, Unable to cast object of type System.Data.Entity.Infrastructure.DbQuery to System.Collections.Generic.IEnumerable

查看:1724
本文介绍了将两个EF查询,无法转换类型System.Data.Entity.Infrastructure.DbQuery的对象System.Collections.Generic.IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体框架查询,每区选出两列,我想Concat的或加入这两个查询的结果结合的原因,

我曾尝试的毗连方法,但它的投掷:

无法投类型的对象

<$p$p><$c$c>'System.Data.Entity.Infrastructure.DbQuery`1[VB$AnonymousType_3`2[System.String,System.String]]'
 输入
 System.Collections.Generic.IEnumerable`1 [VB $ AnonymousType_2`2 [System.String,System.String]

下面是我的code:

 暗淡R =(从PropertyDefinitions在econtext.PropertyDefinitions加入ProductPropertyValues​​在
            econtext.ProductPropertyValues​​在ProductPropertyValues​​.ProductPropDefID等于PropertyDefinitions.PropertyDefID
            加入AdProductDefValues​​在econtext.AdProductDefValues
            在AdProductDefValues​​.PropValueID等于ProductPropertyValues​​.ProductPropValueID
            凡AdProductDefValues​​.AdID =的AdID
            选择PropertyDefinitions.PropertyDefName2,AdProductDefValues​​.DefValue2).Concat(从AdCustomProperties在econtext.AdCustomProperties
            凡AdCustomProperties.AdID =的AdID
            选择AdCustomProperties.PropertyTitle2,AdCustomProperties.PropertyValue2)            GridProperties.DataSource = r.ToArray()
            GridProperties.DataBind()

任何想法的结果两者结合起来的查询?


解决方案

该可枚举不能被连接起来,因为它们是不同的匿名类型,由于具有不同的属性名称。

的一个解决方案是使类型匹配,例如你可以代替这部分:

 选择PropertyDefinitions.PropertyDefName2,AdProductDefValues​​.DefValue2

有了这个

 选择New随着{.Name点= PropertyDefinitions.PropertyDefName2,_
                  .value的= AdProductDefValues​​.DefValue2}

和类似的:

 选择New随着{.Name点= AdCustomProperties.PropertyTitle2,_
                  .value的= AdCustomProperties.PropertyValue2}

I have two Entity Framework queries, each returning two columns, and i would like to concat or join the results of both queries for the reason of binding,

I have tried the Concat method but it's throwing:

Unable to cast object of type

'System.Data.Entity.Infrastructure.DbQuery`1[VB$AnonymousType_3`2[System.String,System.String]]'
 to type
 'System.Collections.Generic.IEnumerable`1[VB$AnonymousType_2`2[System.String,System.String]]

'.

Here is my code:

   Dim r = (From PropertyDefinitions In econtext.PropertyDefinitions Join ProductPropertyValues In
            econtext.ProductPropertyValues On ProductPropertyValues.ProductPropDefID Equals PropertyDefinitions.PropertyDefID
            Join AdProductDefValues In econtext.AdProductDefValues
            On AdProductDefValues.PropValueID Equals ProductPropertyValues.ProductPropValueID
            Where AdProductDefValues.AdID = AdID
            Select PropertyDefinitions.PropertyDefName2, AdProductDefValues.DefValue2).Concat(From AdCustomProperties In econtext.AdCustomProperties
            Where AdCustomProperties.AdID = AdID
            Select AdCustomProperties.PropertyTitle2, AdCustomProperties.PropertyValue2)

            GridProperties.DataSource = r.ToArray()
            GridProperties.DataBind()

Any Ideas to combine the results of both queries?

解决方案

The enumerables cannot be concatenated because they are different anonymous types, due to having different property names.

One solution is to make the types match, e.g. you could replace this part:

Select PropertyDefinitions.PropertyDefName2, AdProductDefValues.DefValue2

With this

Select New With { .Name = PropertyDefinitions.PropertyDefName2, _
                  .Value = AdProductDefValues.DefValue2 }

And similarly:

Select New With { .Name = AdCustomProperties.PropertyTitle2, _
                  .Value = AdCustomProperties.PropertyValue2 }

这篇关于将两个EF查询,无法转换类型System.Data.Entity.Infrastructure.DbQuery的对象System.Collections.Generic.IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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