简体   繁体   English

对象初始化器中的匿名方法

[英]Anonymous Method in Object initializer

I am creating a quiz in which have following class 我正在创建一个具有以下课程的测验

Quiz with properties CorrectOption , WrongOption1 , WrongOption2 , WrongOption3 . Quiz与性能CorrectOptionWrongOption1WrongOption2WrongOption3

in its DTO i have the List<String> Options that will contain all wrong and correct options. 在其DTO我具有List<String> Options ,其中将包含所有错误和正确的选项。

While retrieving the entities i am using object initializer of DTO but don't know how to assign List<String> Options . 在检索实体时,我正在使用DTO的对象初始化程序,但不知道如何分配List<String> Options

I remember we use the anonymous methods to do so. 我记得我们使用匿名方法来做到这一点。

     select new QuestionDTO
                {
                    Category = q.QuizCategory.Text
                    ,
                    CorrectOption = q.CorrectOption
                    ,
                    DifficultyLevel = q.DifficultyLevel.Text
                    ,
                    Points = q.DifficultyLevel.Points.Value
                    ,
                    RewardPCT = q.DifficultyLevel.RewardPCT.Value
                    ,
                    Text = q.Text
                    ,
                    TimerDuration = q.DifficultyLevel.TimerDuration.Value
                    ,
                    Options = (qz) =>
                        {
                            List<string> ops = new List<string>();

                            ops.Add(q.CorrectOption);
                            ops.Add(q.WrongOption1);
                            ops.Add(q.WrongOption2);
                            ops.Add(q.WrongOption3);

                            return new List<string>().Shuffle();
                        }
                };

but it gives following error. 但它给出以下错误。

Cannot convert lambda expression to type 'System.Collections.Generic.List' because it is not a delegate type. 无法将lambda表达式转换为类型'System.Collections.Generic.List',因为它不是委托类型。

UPDATE 更新

For instance i have created a read only property on original entity class to do the work. 例如,我在原始实体类上创建了一个只读属性来完成这项工作。 but plz let me know the better way. 但请让我知道更好的方法。 Thanks 谢谢

UPDATE2 更新2

But it didn't work :p says following on WCFTestClient.exe 但这不起作用:p在WCFTestClient.exe上说以下

The specified type member 'Options' is not supported in LINQ to Entities. LINQ to Entities不支持指定的类型成员'Options'。 Only initializers, entity members, and entity navigation properties are supported. 仅支持初始化程序,实体成员和实体导航属性。

Ran into similar issue. 遇到类似问题。 I was able to get around it by calling ToList() on the query and then a separate query for Select new {... } 我可以通过在查询中调用ToList()然后对Select new {...}进行单独查询来解决此问题。

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

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