Select Clause using System.Linq.Dynamic is returning "'(' expected" fault

c# dynamic-linq linq linq-to-sql

Question

I'm trying to use linq dynamic to create a selec that returns me a specific object. But I'm always getting the same error message: '(' expected

My select code:

var resultCollection = manyPartEntireCollection
                .Select("new MyNamespace.OneToManyViewModel(@0 as OnePartId, Id as ManyPartId, Name as Name, @1.Contains(outerIt.Id) as Enable)", onePartId, manyParIds)
                .Cast<OneToManyViewModel>()
                .ToList()
                ;

Thanks!

1
0
7/5/2017 12:20:49 AM

Accepted Answer

The main question from your example is "what is outerIt?" (@1.Contains(outerIt.Id)) This looks to be just a piece of a larger query. You may want to post up the complete set of expressions.

Based on an assumption of what it looks like you are after: It may be possible within a single Linq statement, but often I hit weird-isms around Linq2Entities/Linq2SQL when it comes to view models and native .Net methods/structures so I tend to default to selecting the data separately to the end formatting.

var resultCollection = manyPartEntireCollection
  .Select( x => new { ManyPartId = x.Id, x.Name })
  .ToList()
  .Select( x => new OneToManyViewModel 
    {
      OnePartId = onePartId,
      ManyPartId = x.ManyPartId,
      Name = x.Name,
      IsEnabled = manyPartIds.Contains(x.ManyPartId)
    }).ToList();

A word of caution about code like this: Initially you may have a reasonable amount of data, but down the road you will encounter potential performance and resource issues with the .ToList() expressions. (No upper limit on # of items.)

update: if DynamicLinq is required, then you can implement the solution offered by dahlbyk on this thread: System.LINQ.Dynamic: Select(" new (...)") into a List<T> (or any other enumerable collection of <T>)

the source for System.Linq.Dynamic can be found here: https://github.com/meatGUY/System.Linq.Dynamic (Updated to meatGUY fork as it looks to be more up to date)

I've created a fork with this implementation available here: https://github.com/StevePy/System.Linq.Dynamic I'll flick over a pull request to meatGUY to see if it's worth incorporating.

I made the modifications and it appears to do what you would expect. No idea why this hasn't been officially incorporated because it makes quite a bit of sense. :)

His instructions are easy enough to follow and you will end up with a call that looks like:

var resultCollection = manyPartEntireCollection
  .Select<OneToManyViewModel>("new (@0 as OnePartId, Id as ManyPartId, Name as Name, @1.Contains(outerIt.Id) as Enable)", onePartId, manyParIds)
  .ToList();

I tested it with a similar object model with the Contains to set a Boolean flag in the view model and it worked a treat. Caveat though is on what that "outerIt" reference goes to.

1
7/6/2017 1:41:15 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow