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

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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 }

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x