List映射到另外一个List

chai1338 2016-05-06 10:19:50


public class ContactsRequest1
{
public List<Contacts1> result_list { get; set; }
}
public class Contacts1
{
public int contactId { get; set; }
public int UserId { get; set; }
public string contactName{ get; set; }
public string contactRelated{ get; set; }
public string contactPhone{ get; set; }
}

public class ContactsRequest2
{
public List<Contacts2> result_list { get; set; }
}
public class Contacts2
{
public int contactId { get; set; }
public int UserId { get; set; }
public string contactName{ get; set; }
public string contactRelated{ get; set; }
public string contactPhone{ get; set; }
}

请问如何把ContactsRequest2.result_list 的值 赋给ContactsRequest1.result_list并且列一一对应
...全文
604 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2016-05-06
  • 打赏
  • 举报
回复
遍历吧。因为对于程序来说,他认为你是2个不同的对象
榮華 2016-05-06
  • 打赏
  • 举报
回复
其实楼上大哥写的ling 也可以实现的。。。。
榮華 2016-05-06
  • 打赏
  • 举报
回复
不知道是不是这样,,,你的问题我读的有点疑问,,总之若能帮到哥哥就更好了
            for (int i = 0; i < ContactsRequest2.result_list.Count; i++)
            {
                for (int j = 0; j < ContactsRequest1.result_list.Count; j++)
                {
                    if (ContactsRequest2.result_list[i].contactId == ContactsRequest1.result_list[j].contactId)
                    {
                        ContactsRequest1.result_list[j].UserId = ContactsRequest1.result_list[i].UserId;
                        ContactsRequest1.result_list[j].contactName = ContactsRequest1.result_list[i].contactName;
                        ContactsRequest1.result_list[j].contactRelated = ContactsRequest1.result_list[i].contactRelated;
                        ContactsRequest1.result_list[j].contactPhone = ContactsRequest1.result_list[i].contactPhone;
                    }
                }
            }
        }
全栈极简 2016-05-06
  • 打赏
  • 举报
回复
var result = from p in request1.result_list from q in request2.result_list where p.contactId == q.contactId select q; List<Contacts2> contacts2 = result.ToList<Contacts2>();
zj25810 2016-05-06
  • 打赏
  • 举报
回复
我以前碰到这种问题是遍历接收的这个List,将里面的每一个对象重新转换为Contacts1。类似于

var list=new List<Contacts1>();
foreach(var C2 in result_list)
{
        var C1=new Contacts1{
               C1.contactId =C2.contactId;
                ...
        }
        list.Add(C1);
}
当然,我这种情况是因为只取C2中的几个重要字段来用。像你这样字段都一样的话,应该可以直接用强转吧。
chai1338 2016-05-06
  • 打赏
  • 举报
回复
引用 1 楼 guwei4037 的回复:
这不是数据库设计,通过C#代码可以直接通过contactId查询出双方匹配的记录。 如果硬要这样设计,数据库中对多对多的关系的处理,一般是引入中间表,这张表只存放Contacts1的contactId和Contacts2的contactId。
这不是数据库设计,就是跨平台传过来这么一个值 我要去接收这个LIST<T> 我问的就是用c#代码如何去接收
全栈极简 2016-05-06
  • 打赏
  • 举报
回复
这不是数据库设计,通过C#代码可以直接通过contactId查询出双方匹配的记录。 如果硬要这样设计,数据库中对多对多的关系的处理,一般是引入中间表,这张表只存放Contacts1的contactId和Contacts2的contactId。
zhujiazhi 2016-05-06
  • 打赏
  • 举报
回复
automapper

62,270

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧