Combine two EF Queries, Unable to cast object of type System.Data.Entity.Infrastructure.DbQuery to System.Collections.Generic.IEnumerable

会有一股神秘感。 提交于 2019-12-01 07:28:14

问题


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?


回答1:


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 }



回答2:


The problem is that each sequence has a different type, so you can't just mash the 2 together as is. If you apply a .Cast<object>() to each one, before the Concat call, it should work as expected.

I think it would be cleaner if you separated the queries in different variables, then it would be a matter of:

DataSource = query1.Cast<object>().Concat(query2.Cast<object>());

Alternatively, you could create a method that returns the results as a non-generic IEnumerable:

private IEnumerable GetData()
{
    var query1 = [query1 logic];
    foreach (var item in query1)
        yield return item;

    var query2 = [query2 logic];
    foreach (var item in query2)
        yield return item;
}
...
DataSource = GetData();

This is not very common (to use untyped IEnumerable like this) but is useful with databinding since it is heavily based in reflection (thus the type of the items in the collection generally doesn't matter).

PS: Sorry for the C# specific syntax, as I don't work with VB and would probably just mess something in the process if I tried to write on that.



来源:https://stackoverflow.com/questions/20788129/combine-two-ef-queries-unable-to-cast-object-of-type-system-data-entity-infrast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!