本文介绍了区分基于使用groupby的两列的记录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 EF6 来获取列表。我有这样一个类:

  public class Province 
{
public string province {set;得到; }
public string provinceCode {set;得到; }
}

Zone class

  namespace InnoviceDomainClass 
{
using System;
使用System.Collections.Generic;

public partial class Zone
{
public string CityCode {get;组; }
public string City {get;组; }
public string Province {get;组; }
public Nullable< int> ProvinceCode {get;组; }
}
}

我使用这个获取我的数据:

 列表< Zone> ListZone = zoneRepository.GetAll()。ToList(); 

我需要区分我的记录:

 列表< Province> ListZoneWithDistinct = ListZone.Select(i => new Province()
{
province = i.Province,
provinceCode = i.ProvinceCode.Value.ToString()
}) .Distinct()ToList();

我认为我的问题是 Distinct() ,我应该根据哪一列应该区分这个函数来告诉这个函数?
但是我的记录没有改变,为什么?和我的记录是一样的

 省代码省
10伊朗
10伊朗
15美国
15美国

我需要的输出:

  provincecode province 
10伊朗
15美国


解决方案

编辑:



是不同的是您的问题,区分Lambda尝试():

 列表< Province> ListZoneWithDistinct = 
ZoneList.GroupBy(x => new {x.Province,x.ProvinceCode})
.Select(grp => new Province()
{
province = grp.First()。Province,
provinceCode = grp.First()。ProvinceCode
})。ToList();

或者您可以尝试以下LINQ / L2E:

 列表<省份> ListZoneWithDistinct = 
(来自ListZone中的lz
选择新的省份()
{
省份= lz.Province,
省份代码= lz.ProvinceCode.Value.ToString()
})。Distinct()。ToList();


I am trying to fetch a list using EF6 .I have a class like this:

public class Province
    {
        public string province { set; get; }
        public string provinceCode { set; get; }
    }

Zone class

namespace InnoviceDomainClass
{
    using System;
    using System.Collections.Generic;

    public partial class Zone
    {
        public string CityCode { get; set; }
        public string City { get; set; }
        public string Province { get; set; }
        public Nullable<int> ProvinceCode { get; set; }
    }
}

I fetch my data using this :

List<Zone> ListZone = zoneRepository.GetAll().ToList();

i need to Distinct my records :

   List<Province> ListZoneWithDistinct = ListZone.Select(i => new Province()
            {
                province = i.Province,
                provinceCode = i.ProvinceCode.Value.ToString()
            }).Distinct().ToList();

I think my problem is Distinct() ,i should tell this function based on which column should be distinct?But my records don't change ;why ?and my records are same

my records is like this

provincecode       province
10                 Iran
10                 Iran
15                 USA
15                 USA

Output that i need:

provincecode       province
10                 Iran
15                 USA
解决方案

EDITED:

Yes Distinct is your problem, to Distinct a Lambda try (See it working here):

List<Province> ListZoneWithDistinct =
               ZoneList.GroupBy(x => new {x.Province, x.ProvinceCode})
               .Select(grp => new Province()
                {
                    province = grp.First().Province,
                    provinceCode = grp.First().ProvinceCode
                }).ToList();

Or you could try the following LINQ / L2E:

List<Province> ListZoneWithDistinct = 
                                    (from lz in ListZone
                                     select new Province()
                                     {
                                         province = lz.Province,
                                         provinceCode = lz.ProvinceCode.Value.ToString()
                                     }).Distinct().ToList();

这篇关于区分基于使用groupby的两列的记录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 16:21