none
linq去重 RRS feed

  • 问题

  •  ID                                  CropCode CropName CropStageCode CropStageName
    032f27f3-142b-491a-88b6-6527a001dda8 01 水稻 03 有穗分化期
    032f27f3-142b-491a-88b6-6527a0025da8 01 水稻 05 灌浆成熟期
    032f27f3-142b-491a-88b6-6527a002dda8 01 水稻 04 抽穗扬花期
    032f27f3-142b-491a-88b6-6537a001bda8 01 水稻 01 苗期
    032f27f3-142b-491a-88b6-6537a001dda8 01 水稻 02 分蘖期
    87bfebde-9f44-4b1e-ae94-b651cf5a8888 03 大豆 06 成熟期
    87bfebde-9f44-4b1e-ae94-b651cf5ab583 03 大豆 03 分枝期
    87bfebde-9f44-4b1e-ae94-b651cf5ab883 03 大豆 04 开花期
    87bfebde-9f44-4b1e-ae94-b651cf5ab888 03 大豆 05 结荚鼓粒期
    87bfebde-9f44-4b1e-ae94-b651cf5aba83 03 大豆 02 幼苗期
    87bfebde-9f44-4b1e-ae94-ba51cf5aba83 03 大豆 01 种子萌发期
    12f5780d-180d-4597-bcc4-e6b95ba43b3a 03 玉米 06 成熟
    72f5780d-180d-4597-bcc4-e6b95ba43b3a 02 玉米 05 吐丝
    79f5780d-180d-4597-bcc4-e6b95ba43b3a 02 玉米 04 开花
    79f5780d-180d-4597-bcc4-e6b95ba4ab3a 02 玉米 03 抽雄
    79f5780d-180d-4597-bcc4-e6b95bc4ab3a 02 玉米 02 拔节
    79f5780d-180d-4597-bcc4-e6b95bc4db3a 02 玉米 01 出苗如上面所示,我查询出来了整个数据,
     var result = cropBll.GetModelList(qB);放在集合result 中,我想把重复的项去掉,并且把这个集合绑定到dropdownlist,
     ddlGrow.DataSource = result ;
            ddlGrow.DataTextField = "CropName";
            ddlGrow.DataValueField = "CropCode";,
    linq怎么写


    please verify my account

    2015年7月23日 4:49

答案

  • 场景代码:

        public class Crop
        {
            public Guid Id { get; set; }
            public string CropCode { get; set; }
            public string CropName { get; set; }
            public string CropStageCode { get; set; }
            public string CropStageName { get; set; }

            public Crop(string inlineCode)
            {
                if(string.IsNullOrEmpty(inlineCode))
                    throw new ArgumentNullException(inlineCode);
                var temp = inlineCode.Split(new [] { " " }, StringSplitOptions.RemoveEmptyEntries);

                if (temp.Count() == 5)
                {
                    Id = Guid.Parse(temp[0]);
                    CropCode = temp[1];
                    CropName = temp[2];
                    CropStageCode = temp[3];
                    CropStageName = temp[4];
                }
                else
                {
                    throw new ArgumentException("inlineCode is not vaild, need 5 part in initialization");
                }
            }
        }

        public static class Program
        {
            public static void Main()
            {
                ICollection<Crop> crops = new List<Crop>();

                crops.Add(new Crop("032f27f3-142b-491a-88b6-6527a001dda8 01 水稻 03 有穗分化期"));
                crops.Add(new Crop("032f27f3-142b-491a-88b6-6527a0025da8 01 水稻 05 灌浆成熟期"));
                crops.Add(new Crop("032f27f3-142b-491a-88b6-6527a002dda8 01 水稻 04 抽穗扬花期"));
                crops.Add(new Crop("032f27f3-142b-491a-88b6-6537a001bda8 01 水稻 01 苗期"));
                crops.Add(new Crop("032f27f3-142b-491a-88b6-6537a001dda8 01 水稻 02 分蘖期"));
                crops.Add(new Crop("87bfebde-9f44-4b1e-ae94-b651cf5a8888 03 大豆 06 成熟期"));
                crops.Add(new Crop("87bfebde-9f44-4b1e-ae94-b651cf5ab583 03 大豆 03 分枝期"));
                crops.Add(new Crop("87bfebde-9f44-4b1e-ae94-b651cf5ab883 03 大豆 04 开花期"));
                crops.Add(new Crop("87bfebde-9f44-4b1e-ae94-b651cf5ab888 03 大豆 05 结荚鼓粒期"));
                crops.Add(new Crop("87bfebde-9f44-4b1e-ae94-b651cf5aba83 03 大豆 02 幼苗期"));
                crops.Add(new Crop("87bfebde-9f44-4b1e-ae94-ba51cf5aba83 03 大豆 01 种子萌发期"));
                crops.Add(new Crop("12f5780d-180d-4597-bcc4-e6b95ba43b3a 03 玉米 06 成熟"));       //<==这里的CropCode不对
                crops.Add(new Crop("72f5780d-180d-4597-bcc4-e6b95ba43b3a 02 玉米 05 吐丝"));
                crops.Add(new Crop("79f5780d-180d-4597-bcc4-e6b95ba43b3a 02 玉米 04 开花"));
                crops.Add(new Crop("79f5780d-180d-4597-bcc4-e6b95ba4ab3a 02 玉米 03 抽雄"));
                crops.Add(new Crop("79f5780d-180d-4597-bcc4-e6b95bc4ab3a 02 玉米 02 拔节"));
                crops.Add(new Crop("79f5780d-180d-4597-bcc4-e6b95bc4db3a 02 玉米 01 出苗"));

                var dropdownlist = from c in crops
                    select new {id = c.CropCode, text = c.CropName};

                //这里去重

                foreach (var item in dropdownlist.Distinct())
                {
                    Console.WriteLine(string.Format("{0}, {1}", item.id, item.text
                ));
                }
            }
        }

    //===========执行结果===========

    01, 水稻
    03, 大豆
    02, 玉米
    请按任意键继续. . .


    相关链接:https://msdn.microsoft.com/zh-cn/library/bb348436(v=vs.110).aspx
    2015年7月23日 7:50