OData / OData/WebApi

Filtering by OpenTypes doesn't work with Linq to Entity

Open
#487 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P3
Dominant language
C#
Stars
864
Forks
467
PR merge metrics
No merged PRs in 30d

Description

I store the data for the open types in the database, and I implemented dynamic initialization for them.
I'm using the latest version of OData v4 and EF 6.

My model:

public class ExampleDto
{
        private IEnumerable<CustomAttribute> _customAttributes { get; set; }
        public IEnumerable<CustomAttribute> Attributes
        {
            get { return _customAttributes ?? (_customAttributes = new List<CustomAttribute>()); }
            set { _customAttributes = value; }
        }

        private IDictionary<string, object> _dynamicAttributes { get; set; }
        public IDictionary<string, object> DynamicAttributes
        {
            get
            {
                _dynamicAttributes = new Dictionary<string, object>();
                if (Attributes != null && Attributes.Any())
                {
                    _dynamicAttributes = Attributes.ToDictionary(s => s.Name, s => s.Value);
                }
                return _dynamicAttributes;
            }
            set { _dynamicAttributes = value; }
        }
}

"Attributes" field initialization:

var attributes = _exampleRepository.Select(s => new ExampleDto() 
{ 
   Attributes = new CustomAttribute()
   {
     Name = s.Name,
     Value = s.Value 
   }
}

return Ok(attributes)

This works fine, but if I try to use OData filters, Web API to throw next exception:
"Unknown LINQ expression of type 'Index'"

This exception thrown only with filtering by open types.
Also, this method work if I use ToList() function before applies Odata filters.
But i can't use this way because, if used ToLIst() before filtering then i get all data from DB.
This is not a good solution for performance.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the repository Select/Ok path and the OData filter application; reproduce the “Unknown LINQ expression of type 'Index'” failure using the shown ExampleDto and dynamic attributes. Compare the query with and without ToList(), and consider the issue done when filtering remains provider-translatable without materializing all database rows.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.