怎么根据特性判断组合key是否重复

-一个大坑 2019-12-06 03:34:52

uploadDataList
List<string> keyList = new List<string>();
foreach (PropertyInfo pi in propertys)
{
KeyAttribute keyAttribute = (KeyAttribute)Attribute.GetCustomAttribute(t.GetType(), typeof(KeyAttribute));
if (keyAttribute != null)
{
keyList.Add(t.GetType().Name);
}
}
...全文
225 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
-一个大坑 2019-12-09
  • 打赏
  • 举报
回复
引用 1 楼 github_36000833 的回复:
没能理解你代码的用意。不过要判断重复,可以用HashSet。
var keyList = new HashSet<string>();
foreach (PropertyInfo pi in properties)
{
    var keyAttribute = (KeyAttribute)Attribute.GetCustomAttribute(t.GetType(), typeof(KeyAttribute));
    if (keyAttribute != null)
    {
        if (!keyList.Add(t.GetType().Name))
        {
            throw new InvalidOperationException($"Key '{t.GetType().Name}'重复");
        }
    }
}
我的那段代码是不知道怎么写随便写的,不具备参考价值,只是把key特性的name都找出来了 比如我有uploadDataList,需要反射找带key特性的属性,然后判断key值组合是否重复,重复的就提取list出来 var repetitionList=uploadDataList.GroupBy(m=>new { NCMP = m.NCMP, PINO = m.PINO }) .Where(g=>g.Count()>1).Select(y=>y.Key).ToList();
github_36000833 2019-12-07
  • 打赏
  • 举报
回复
没能理解你代码的用意。不过要判断重复,可以用HashSet。
var keyList = new HashSet<string>();
foreach (PropertyInfo pi in properties)
{
    var keyAttribute = (KeyAttribute)Attribute.GetCustomAttribute(t.GetType(), typeof(KeyAttribute));
    if (keyAttribute != null)
    {
        if (!keyList.Add(t.GetType().Name))
        {
            throw new InvalidOperationException($"Key '{t.GetType().Name}'重复");
        }
    }
}

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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