如何用C#中的多重值排列键 [英] How to arrange key with multipal value in C#

查看:71
本文介绍了如何用C#中的多重值排列键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有Name和contect的Employ类。我必须删除重复的name.Output应该是Ram和他所有的竞争编号。不想每次都重复名字。



I have one Employ class which has Name and contect.I have to remove duplicate name.Output should be Ram and his all contect number like that for all. Dont want to repeat name every time.

       List<Employ> emp = new List<Employ>();
            Employ e = new Employ();
            emp.Add(new Employ { Name = "Ram", Contect = "5454454" });
            e.Name = "Ravi";
            e.Contect = "458794";
            emp.Add(new Employ { Name = "Ravi", Contect = "4578456" });

            e.Name = "jack";
            e.Contect = "564545655";
            emp.Add(new Employ { Name = "Jack", Contect = "457844544" });
            e.Name = "Ram";
            e.Contect = "457875";
            emp.Add(new Employ { Name = "Ram", Contect = "45784554" });
            e.Name = "Ravi";
            e.Contect = "5467846554";
            emp.Add(new Employ { Name = "Ravi", Contect = "5784545" });
            e.Name = "jack";
            e.Contect = "457855664";
            emp.Add(new Employ { Name = "Jack", Contect = "4547787787" });

}
 class Employ
    {
       public string Name { get; set; }
        public string Contect { get; set; }


   
    }





我的尝试:



我删除了重复的名称条目



What I have tried:

I removed the duplicate name entry

public static  void RemoveDuplicate(List<Employ> name) {
    List<string > str=null;
    str = name.Select(x => x.Name).Distinct().ToList();



但不知道如何将值联系到此


but no idea how to contact value into this

推荐答案

1)变量e分配多次,但从未使用?



2)我会这样开始



1) the variable e is assigned multiple times but never used?

2) I would start like this

void Main()
{
	List<Employ> emp = new List<Employ>();
	emp.Add(new Employ { Name = "Ram", Contect = "5454454" });
	emp.Add(new Employ { Name = "Ravi", Contect = "4578456" });
	emp.Add(new Employ { Name = "Jack", Contect = "457844544" });
	emp.Add(new Employ { Name = "Ram", Contect = "45784554" });
	emp.Add(new Employ { Name = "Ravi", Contect = "5784545" });
	emp.Add(new Employ { Name = "Jack", Contect = "4547787787" });

    //emp.Dump(); .Dump() is a linqpad command
	
	var employesDistinctByName = emp.GroupBy(e => e.Name).Select(y => y.First());
	
	//employesDistinctByName.Dump();
}





这样你就可以在不破坏列表的情况下摆脱双重名称。但是,您没有指定如何处理Name和Contect的组合(如果名称相同,Contect不同,如何处理)



so you get rid of the double names without destroying the list. However you did not specify how to handle the combination of Name and Contect (if the name is the same, the Contect different, how to handle this)


这篇关于如何用C#中的多重值排列键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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