2

I'm having an issue in Linq and have an object like this one:

  {"Id": "",
  "tDate": "2021-02-08T13:15:59.5419262Z",
  "innerObject": [
    {
      "innerObjectId": "",
      "tDate": "2021-02-08T13:15:59.528643Z"
    }
  ],
  ...};

This is connected to an api, and when it's called I want to query this object and return all the attributes in ObjectDTO while changing the tDate in case there's a date in the innerObject (I know that condition works, so no need to worry with that):

this.query = cosmosDbProvider.GetContainer().GetItemLinqQueryable<ObjectDTO>().AsQueryable()
                .Where(t => someconditions)
                .Select(o => { o.tDate = o.innerOject.Where(t => t.innerObjectId == input.innerObjectId).Select(t => t.tDate).ToArray()[0] ?? o.tDate; 
                               return o; });

However, I can't seem to make this work. The errors I'm getting are 'A lambda expression with a statement body cannot be converted to an expression tree' and 'An expression tree may not contain an assignment operator'. The other option I've tried is manually assigning each variable of ObjectDTO, but that looks awful.

Does anyone have any ideas?

nateirvin
  • 1,173
  • 1
  • 10
  • 28
J.Doe
  • 529
  • 4
  • 14

2 Answers2

0

I would try to use a "foreach" in that case with the "where" results.

Also if you want to use LINQ you may check this answer.

Is nice to remember that if you are working with an IQueryable you should know that the "ToList" method would trigger the Entity Framework query in the database before going to your "ForEach". In that case, try to have a look at this LINQ to SQL syntax

-1

There are two enumerations you need to be aware of...one is the IEnumerable which is a concreate representation of the data and is in memory for access. There other is IQueryable which is not a hard representation, but a set of possibilities for what the data will look like and is sent to the database and not in memory.

But the method Where returns an IQueryable which is fine and can be chained with other IQueryable returns, but the Select is asking to change the shape of existing data (as IEnumerable).

Put in a ToList() (which will make the IQueryable(s) to be executed and return a concrete List<T> of objects) after the Where( ... ); and that will provide a valid data set to the Select operate on.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Thank you for your answer! I've tried this and the execution stops and I get the following error "To execute LINQ query please set allowSynchronousQueryExecution true or use GetItemQueryIterator to execute asynchronously". Also, wouldn't the ToList approach be too 'expensive' in terms of performance? – J.Doe Feb 10 '21 at 18:02
  • Performance...are you running an application on a 286 machine with 512K? Sounds funny but it is true, modern computers have literally gigs of ram. Unless your query is pulling down a boat load of data, it shouldn't be a problem. If you need a specific look and feel to the data, I recommend that you create a stored procedure to do the heavy lifting and return a set of a DTO that you need. One can use sprocs with EF or other ORMs. – ΩmegaMan Feb 10 '21 at 18:05