C# 如何返回1个属性不重复的列表?

C# 如何返回1个属性不重复的列表?,c#,list,unique,hashset,C#,List,Unique,Hashset,我有一个从数据库中提取州和城市列表的方法。这些州是独一无二的,但在那个州可以有很多城市。我的方法目前所做的是将每个州和城市对作为单个项返回。我需要它做的是拥有一个拥有许多城市的州 目前返回 哦,辛辛那提 哦-克利夫兰 哦,芬德利 印第安纳波利斯 我需要它来归还什么 哦,辛辛那提,克利夫兰,芬德利 印第安纳波利斯 模型 存储库 public HashSet<Location> getlocation() { HashSet<Location> myH

我有一个从数据库中提取州和城市列表的方法。这些州是独一无二的,但在那个州可以有很多城市。我的方法目前所做的是将每个州和城市对作为单个项返回。我需要它做的是拥有一个拥有许多城市的州

目前返回

哦,辛辛那提

哦-克利夫兰

哦,芬德利

印第安纳波利斯

我需要它来归还什么

哦,辛辛那提,克利夫兰,芬德利

印第安纳波利斯

模型

存储库

public HashSet<Location> getlocation()
    {
        HashSet<Location> myHashset = new HashSet<Location>();

        const string storedProc = "someProc";

        dynamic locations;


        using (var conn = DbFactory.myConnection())
        {
            locations = conn.Query(storedProc, commandType: CommandType.StoredProcedure);

        }

        foreach (var location in locations)
        {

                myHashset.Add(new location{State = location.state,City = location.city});


        }
          return myHashset
    }
public HashSet getlocation()
{
HashSet myHashset=新HashSet();
常量字符串storedProc=“someProc”;
动态定位;
使用(var conn=DbFactory.myConnection())
{
locations=conn.Query(storedProc,commandType:commandType.StoredProcedure);
}
foreach(位置中的var位置)
{
添加(新位置{State=location.State,City=location.City});
}
返回myHashset
}
这应该可以

var Result = myHashset.GroupBy(r => r.State)
        .Select(g => new Location
        {
            State = g.Key,
            city = String.Join(", ", g.Select(r => r.city))
        });
也许您不想将其存储到新的
位置
对象中。我会用字典

Dictionary<string,string> Result = myHashset.GroupBy(r => r.State)
.ToDictionary(g => g.Key, g => String.Join(", ", g.Select(r => r.city)));
更新-词典

Dictionary<string,string> Result = myHashset.GroupBy(r => r.State)
.ToDictionary(g => g.Key, g => String.Join(", ", g.Select(r => r.city)));
Dictionary Result=myHashset.GroupBy(r=>r.State)
.ToDictionary(g=>g.Key,g=>String.Join(“,”,g.Select(r=>r.city));
这应该可以

var Result = myHashset.GroupBy(r => r.State)
        .Select(g => new Location
        {
            State = g.Key,
            city = String.Join(", ", g.Select(r => r.city))
        });
也许您不想将其存储到新的
位置
对象中。我会用字典

Dictionary<string,string> Result = myHashset.GroupBy(r => r.State)
.ToDictionary(g => g.Key, g => String.Join(", ", g.Select(r => r.city)));
更新-词典

Dictionary<string,string> Result = myHashset.GroupBy(r => r.State)
.ToDictionary(g => g.Key, g => String.Join(", ", g.Select(r => r.city)));
Dictionary Result=myHashset.GroupBy(r=>r.State)
.ToDictionary(g=>g.Key,g=>String.Join(“,”,g.Select(r=>r.city));

[EDIT]:再次阅读您的问题后,这可能不是您想要的。但是,如果您想要一个仅按州(而不是按州和城市)区分的哈希集,那么可以随意使用:

又快又脏:
重写位置的equal和getHashcode方法:

public override bool Equals(Location other)
{
    return this.State.Equals(other.State);
}

public override int GetHashCode()
{
    return this.State.GetHashCode();
}
更干净:
使用IEqualityComparer:

public class StateComparer : IEqualityComparer<Location>
{
    public bool Equals(Location x, Location y)
    {
        if (ReferenceEquals(x, y))
            return true;

        if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
            return false;

        return Equals(x.State, y.State);
    }

    public int GetHashCode(Location obj)
    {
        if (ReferenceEquals(obj, null))
            return 0;

        if (ReferenceEquals(obj.State, null))
            return 0;

        return obj.State.GetHashCode();
    }
}
公共类状态比较器:IEqualityComparer
{
公共布尔等于(位置x,位置y)
{
if(ReferenceEquals(x,y))
返回true;
if(ReferenceEquals(x,null)| | ReferenceEquals(y,null))
返回false;
返回等于(x状态,y状态);
}
公共int GetHashCode(位置obj)
{
if(ReferenceEquals(obj,null))
返回0;
if(ReferenceEquals(obj.State,null))
返回0;
返回obj.State.GetHashCode();
}
}
然后使用以下命令创建哈希集:

HashSet<Location> myHashset = new HashSet<Location>(new StateComparer());
HashSet myHashset=newhashset(newstatecomparer());

[EDIT]:再次阅读您的问题后,这可能不是您想要的。但是,如果您想要一个仅按州(而不是按州和城市)区分的哈希集,那么可以随意使用:

又快又脏:
重写位置的equal和getHashcode方法:

public override bool Equals(Location other)
{
    return this.State.Equals(other.State);
}

public override int GetHashCode()
{
    return this.State.GetHashCode();
}
更干净:
使用IEqualityComparer:

public class StateComparer : IEqualityComparer<Location>
{
    public bool Equals(Location x, Location y)
    {
        if (ReferenceEquals(x, y))
            return true;

        if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
            return false;

        return Equals(x.State, y.State);
    }

    public int GetHashCode(Location obj)
    {
        if (ReferenceEquals(obj, null))
            return 0;

        if (ReferenceEquals(obj.State, null))
            return 0;

        return obj.State.GetHashCode();
    }
}
公共类状态比较器:IEqualityComparer
{
公共布尔等于(位置x,位置y)
{
if(ReferenceEquals(x,y))
返回true;
if(ReferenceEquals(x,null)| | ReferenceEquals(y,null))
返回false;
返回等于(x状态,y状态);
}
公共int GetHashCode(位置obj)
{
if(ReferenceEquals(obj,null))
返回0;
if(ReferenceEquals(obj.State,null))
返回0;
返回obj.State.GetHashCode();
}
}
然后使用以下命令创建哈希集:

HashSet<Location> myHashset = new HashSet<Location>(new StateComparer());
HashSet myHashset=newhashset(newstatecomparer());
您可以在
State
属性上使用
GroupBy()
。您可以在
State
属性上使用
GroupBy()