1

Currently, I'm working with an IQueryable() and cannot get part of the query to work. I'm getting a System.ArgumentException with a description that is in the title of this post.

I'll start with the TSQL query that I need but in IQueryable() code:

 SELECT w.*,wr.*,c.* 
   FROM WorldRegion w
   JOIN WorldRegionResource wr
     ON w.Id = wr.Id
   JOIN Culture c
     ON wr.CultureId = c.Id
  WHERE c.CultureName = 'en-US'

Now I'll break the code:

Here's the Entity structure:

public class WorldRegion
{
    public virtual ICollection<WorldRegionResource> WorldRegionResources { get; set; }
}
public class WorldRegionResource
{
    public Culture Culture { get; set; }
}
public class Culture
{
    public string CultureName { get; set; }
}

Here's a working query (with other expressions removed for brevity), but I need to add a filter to this as this returns ALL available WorldRegionResources for the WorldRegion and I want to return ONE:

IQueryable<WorldRegion> query = repository.Queryable();

query.Include(w => w.WorldRegionResources.Select(wr => wr.Culture));

I need to add a filter:

Where Culture.CultureName == "en-US"

Initially I tried changing the query.Include (which threw the exception):

query.Include(w => w.WorldRegionResources.Where(wr => wr.Culture.CultureName == "en-US"));

I've been searching for a solution, but with no success. I've even tried building an anonymous query:

query.Include(w => w.WorldRegionResources.Select(wr => wr.Culture));
query.Select(w => new
{
    WorldRegion = w,
    WorldRegionResource = w.WorldRegionResources.Select(wr => new
    {
        WorldRegionResource = wr,
        Culture = wr.Culture.CultureName == "en-US"
    })
});

But this produced the same result as the initial query. SQL Server Profiler doesn't even recognize the 'en-US' parameter.

Thank you in advance for your time and any assistance is appreciated,

Ronnie

Ronnie
  • 287
  • 1
  • 3
  • 8
  • It might be because you didnt called ToList() – Yashveer Singh Jan 22 '17 at 16:25
  • can you tell me this query return you data or query ?IQueryable query = repository.Queryable(); query.Include(w => w.WorldRegionResources.Select(wr => wr.Culture)); – Yashveer Singh Jan 22 '17 at 16:38
  • if no result then answer is to call ToList() – Yashveer Singh Jan 22 '17 at 16:39
  • 2
    That's not the problem. He has a WHERE condition inside the Include which is not allowed. – Steve Greene Jan 22 '17 at 16:40
  • 1
    http://stackoverflow.com/questions/15980665/ef-lambda-the-include-path-expression-must-refer-to-a-navigation-property – Steve Greene Jan 22 '17 at 16:45
  • As I mentioned, I removed expressions for brevity. I just show the initialization and the include that requires filtering. But, to note, I do finish with ToList() – Ronnie Jan 22 '17 at 19:30
  • Steve, I came across this post and tried following the answer. But it doesn't work for me. Include(c => c.Modules) and then .Single(c => !c.Module.IsDeleted), I don't know how this was an answer. Even though I need a list, I tried this approach I was only able to select the Plural 'WorldRegionResources' so when I duplicated it, the result was Include(w => w.WorldRegionResources).Single(w => w.WorldRegionResource'S') – Ronnie Jan 22 '17 at 20:16

1 Answers1

-1

I believe that you have to tell the enities which entity they map to.

Try adding the foreign key attribute to the foreign key id.

public class WorldRegion
{
    public virtual ICollection<WorldRegionResource> WorldRegionResources { get; set; }
}
public class WorldRegionResource
{
    [ForeignKey(nameof(Culture))]
    public int CultureId { get; set; }
    public virtual Culture Culture { get; set; }
}
public class Culture
{
    public string CultureName { get; set; }
}
Guardien
  • 1
  • 2