0

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!

André Cristino
  • 95
  • 1
  • 14
  • Is there any specific reason to use dynamic linq for this small straightforward query? – Krishna Jul 05 '17 at 02:02
  • "Id" and "Name" are properties that will change according to the entity. It is just an example, but this properties are parametrized. If I use the dynamic way it works fine, but when I try to set a predefined class, as the case of "OneToManyViewModel", occurs this error. – André Cristino Jul 05 '17 at 15:48

1 Answers1

1

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.

Steve Py
  • 26,149
  • 3
  • 25
  • 43
  • when I try to make by the dynamic mode, it works fine. But when I try to set a type, as "OneToManyViewModel" class, the error occurs. It looks like the error is in the "new MyNamespace.OneToManyViewModel (". Also, as I replied above to Kishna, I need the code to be dynamic and return the this specified type. Thx – – André Cristino Jul 05 '17 at 15:51
  • I think I found the solution to what you're looking for, I've updated my answer with the details and links to how to implement a generic return type on Dynamic Linq Select. – Steve Py Jul 05 '17 at 22:21