如何选择新的对象集属性=其他属性值 [英] How to select new object set property = other property value

查看:99
本文介绍了如何选择新的对象集属性=其他属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我英语不好,我在标题设置上有困难.另外,我也不知道该关键字能够找到我所需要的东西.

I'm not good at English, I have difficulty in title setting. And also so I do not know the keyword to be able to find what I need.

我想要以下内容:

var newList = listUsers.Select(x => new { x.Name = x.Old});

但是我收到这样的错误:

But I get an error like this:

无效的匿名类型成员声明符.匿名类型成员必须 通过成员分配,简单名称或成员访问权限进行声明.

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

注意:我不需要以下内容

var newList = listUsers.Select(x => new { Name = x.Name, Old = x.Old});

更新:我的最终目标是输出这样的格式:

Update: My final goal would be to output the format like this:

var arr = [{
            John Doe: 19,
            David: 23,
            David: 26
        }, {

            John Doe: 27,
            David: 22,
            David: 24
        }];

用户名列表是动态的

推荐答案

只需从匿名类型成员名称的第一个中删除x:

Just remove the x from the first of the Anonymous type members's name:

var newList = listUsers.Select(x => new { Name = x.NameNew});

或者如果您想用NameNew更新所有Name属性,则也需要这样的return x;(在这种情况下,请勿使用new关键字):

Or if you want to update all Name property with NameNew you need the return x; also like this (don't use the new keyword in this case):

var newList = listUsers.Select(x => { x.Name = x.NameNew;return x; });

这篇关于如何选择新的对象集属性=其他属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆