简体   繁体   English

使用System.Linq.Dynamic选择子句返回“'('预期”错误

[英]Select Clause using System.Linq.Dynamic is returning “'(' expected” fault

I'm trying to use linq dynamic to create a selec that returns me a specific object. 我正在尝试使用linq dynamic创建一个返回给我特定对象的选择。 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! 谢谢!

The main question from your example is "what is outerIt?" 您的示例的主要问题是“ outerIt是什么?” ( @1.Contains(outerIt.Id) ) This looks to be just a piece of a larger query. @ 1.Contains(outerIt.Id) )这似乎只是一个较大查询的一部分。 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. 基于以下假设:在单个Linq语句中可能会出现这种情况,但是在查看模型和本机.Net方法/结构时,我经常碰到Linq2Entities / Linq2SQL的怪异现象,因此我倾向于默认情况下,将数据单独选择为最终格式。

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. 关于这样的代码,请谨慎使用:最初,您可能拥有合理数量的数据,但是一路走来,.ToList()表达式会遇到潜在的性能和资源问题。 (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>) 更新:如果需要DynamicLinq,则可以在以下线程上实施dahlbyk提供的解决方案: System.LINQ.Dynamic:Select(“ new(...)”)放入List <T>(或任何其他可枚举的collection) <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) 可在以下位置找到System.Linq.Dynamic的源: https : //github.com/meatGUY/System.Linq.Dynamic (已更新为meatGUY分支,因为它看起来是最新的)

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. 我已经创建了一个具有此实现的fork,可以在这里找到: https : //github.com/StevePy/System.Linq.Dynamic我将轻拂对meatGUY的拉取请求,以查看是否值得合并。

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. 但是,请注意“ outerIt”引用的含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM