Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: InvalidCastException on Where with Any #7657

Closed
NoFear23m opened this issue Feb 18, 2017 · 4 comments
Closed

Query: InvalidCastException on Where with Any #7657

NoFear23m opened this issue Feb 18, 2017 · 4 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@NoFear23m
Copy link

NoFear23m commented Feb 18, 2017

Hy all

In my model i have the Entity Businesspartner. The Businesspartner have a Property "Addresses"
Address have a Property ZipCity from type CityZip
CityZip have a Property PostCode

I will make a search, but if i search with the postcode or the City i get the following error:
System.InvalidCastException: Das Objekt des Typs "<SelectManyIterator>d__162[Microsoft.EntityFrameworkCore.Storage.Internal.InMemoryTableSnapshot,Microsoft.EntityFrameworkCore.Storage.ValueBuffer]" kann nicht in Typ "System.Collections.Generic.IEnumerable1[SPS.DMS.Model.Common.Address]" umgewandelt werden.

I have attached an Image from my model.
screenshotmodel

Here ist my Querycode:

` Dim bpQuery As IQueryable(Of BusinessPartner) = From bp As BusinessPartner In Ctx.BusinessPartners.Include(Function(i) i.Addresses).ThenInclude(Function(i1) i1.ZipCity)
Select bp

    bpQuery = bpQuery.AsNoTracking

    'Gelöschte aus suche ausschliessen?
    If Not includeDeleted Then bpQuery.Where(Function(b) b.DeletedFlag = False)

    'Wenn BP Nummer dann ist ergebniss klar
    If bpNumber > 0 Then
        bpQuery = bpQuery.Where(Function(b) b.BusinessPartnerId = bpNumber)
        Return bpQuery.ToList
    End If

            Dim nameStringList As List(Of String) = namestring.ToLower.Split(" ").ToList

        For i As Integer = nameStringList.Count - 1 To 0 Step -1
            If nameStringList(i) = " " OrElse nameStringList(i) = "" Then nameStringList.RemoveAt(i)
        Next

        bpQuery = bpQuery.Where(Function(b) (b.FirstName IsNot Nothing AndAlso nameStringList.Any(Function(l) b.FirstName.ToLower.Contains(l))) OrElse
          (b.MiddleName IsNot Nothing AndAlso nameStringList.Any(Function(l) b.MiddleName.ToLower.Contains(l))) OrElse
           (b.LastName IsNot Nothing AndAlso nameStringList.Any(Function(l) b.LastName.ToLower.Contains(l))))


    End If

    If Not String.IsNullOrEmpty(firstname) Then bpQuery = bpQuery.Where(Function(b) b.FirstName.ToLower.Contains(firstname.ToLower))
    If Not String.IsNullOrEmpty(lastname) Then bpQuery = bpQuery.Where(Function(b) b.LastName.ToLower.Contains(lastname.ToLower))
    If Not String.IsNullOrEmpty(taxnumber) Then bpQuery = bpQuery.Where(Function(b) b.TaxNumber.ToLower.Contains(taxnumber.ToLower))
    If Not String.IsNullOrEmpty(matchcode) Then bpQuery = bpQuery.Where(Function(b) b.Matchcode.ToLower.Contains(matchcode.ToLower))

    **If postcode IsNot Nothing Then
        bpQuery = bpQuery.Where(Function(plz) plz.Addresses.Any(Function(a) a.ZipCity.ZipCode = postcode))
    End If
    If city IsNot Nothing Then
        bpQuery = bpQuery.Where(Function(x) x.Addresses.Any(Function(y) y.ZipCity.City.ToLower.Contains(city.ToLower)))
    End If**

    If excludeCompanys Then
        bpQuery = bpQuery.Where(Function(b) b.IsCompany = False)
    End If
    If excludePersons Then
        bpQuery = bpQuery.Where(Function(b) b.IsCompany = True)
    End If

    Dim resultList As List(Of BusinessPartner) = bpQuery.ToList

    Return resultList`

What is my mistake??

Thanks in advance for your time.
Best Regards
Sascha

@rowanmiller
Copy link
Contributor

EF Team Triage: This issue is lacking enough information for us to be able to effectively triage it. In particular, it is missing the following information requested in the new issue template. Can you please provide this information?

Steps to reproduce

Ideally include a complete code listing that we can run to reproduce the issue.
Alternatively, you can provide a project/solution that we can run.

BTW we're not just doing this to be mean 😄... we get a lot traffic on this project and it takes time to attempt to reproduce an issue based on fragments of information. In addition, our attempt is often unsuccessful as the exact conditions required to hit the issue are often not explicitly included in the code provided. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we ask that folks give us a self contained way to reproduce an issue.

For a guide on submitting good bug reports, read Painless Bug Tracking.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

@rowanmiller
Copy link
Contributor

Can you also let us know what version of EF Core you are using

@NoFear23m
Copy link
Author

OK, sorry.
My Version is 1.1.0

OK, i will make a testsolution and load it up in the next days. At the moment the code is in a big projekt with totaly 43 projects in my solutionexplorer.

btw: today i have changed this
If city IsNot Nothing Then bpQuery = bpQuery.Where(Function(x) x.Addresses.Any(Function(y) y.ZipCity.City.ToLower.Contains(city.ToLower))) End If

to this
If city IsNot Nothing Then bpQuery = From b As BusinessPartner In bpQuery Join a As Address In Ctx.Addresses On b.BusinessPartnerId Equals a.BusinessPartnerId Where a.ZipCity.City.Contains(city) Select b End If

Now it will work. Maybe this also help you.

Sorry for my mistake :-(
Greets
Sascha

@maumar
Copy link
Contributor

maumar commented Apr 10, 2017

Verified that this works in current bits, closing

@maumar maumar closed this as completed Apr 10, 2017
@maumar maumar added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed type-investigation labels Apr 10, 2017
@divega divega added the type-bug label May 9, 2017
@ajcvickers ajcvickers changed the title InvalidCastException on Where with Any Query: InvalidCastException on Where with Any May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants