开发者

Why cant I use lambda when serializing DataContract?

开发者 https://www.devze.com 2023-01-19 07:55 出处:网络
Made som mo开发者_JAVA技巧ck code below to illustrate my example. The problem is the lambda expression. If I leave it as in the code example it will not serialize when I try to call the service. Howev

Made som mo开发者_JAVA技巧ck code below to illustrate my example. The problem is the lambda expression. If I leave it as in the code example it will not serialize when I try to call the service. However if I type .ToList() after the lambda it serializes as it should.

Why is that? I can't see why the code below should not work... Anyone care to enlighten me? :)

var list = new EntityPerson
               {
                   Names = modelPerson.Names.Select(
                                     n => new EntityName
                                              {
                                                   Text = n.Text
                                              })
                }


That's because of the deferred execution. You're not storing the result of the lambda execution, but rather the expression tree or lambda itself, which would need to serialize a reference (!) to the modelPerson.

http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx and many more show the "problems" associated with this. (Google for "deferred execution .net" for more.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号