Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Linq GroupBy-具有特定标准_C#_Linq - Fatal编程技术网

C#Linq GroupBy-具有特定标准

C#Linq GroupBy-具有特定标准,c#,linq,C#,Linq,我有下表要分组 Id NameId ValueId 1 1 4 1 10 18 1 9 15 2 1 4 2 10 17 2 9 0 3 1 5 3 9 16 3 10 18 4 1 5 4 10 18 4 9

我有下表要分组

Id NameId ValueId 1 1 4 1 10 18 1 9 15 2 1 4 2 10 17 2 9 0 3 1 5 3 9 16 3 10 18 4 1 5 4 10 18 4 9 16 5 1 4 5 10 17 5 9 0 Id名称Id值Id 1 1 4 1 10 18 1 9 15 2 1 4 2 10 17 2 9 0 3 1 5 3 9 16 3 10 18 4 1 5 4 10 18 4 9 16 5 1 4 5 10 17 5 9 0 对于所有对应的NameId,结果应与具有类似ValueId的Id分组

输出

GroupId Id ValueId fed1afcc-a778-48ef-9ee5-4b70886ce67c 1 4,18,15 a31055df-2e4e-472e-9301-e0e0a4e99f1e 2,5 4,17,0 8b9b3dca-4ce0-4cae-a870-1d1026bd608a 3,4 5,18,16 组Id值Id fed1afcc-a778-48ef-9ee5-4b70886ce67c 1 4,18,15 a31055df-2e4e-472e-9301-e0e0a4e99f1e 2,5 4,17,0 8b9b3dca-4ce0-4cae-a870-1d1026bd608a 3,4 5,18,16 我实际上不需要将它们连接起来,它只是表示输出应该如何分组

我使用嵌套for循环完成了一个工作实现,它将每个实体与后续实体进行比较,并检查所有值。我是否做了一些可以在Linq中简单实现的事情

或者如何在Linq中实现这一点

我当前代码的最低版本

  var tableResult = _repository.GetData().OrderBy(x =>x.NameId).ToList();
  var ids = tableResult.Select(x => x.id).Distinct().ToList();
  var listOfProductGroup = new List<ProductGroup>();
  for (int i = 0; i < ids.Count; i++)
  {   
       var currentId = ids[i];
       var currentEntity = tableResult.Where(x => x.id == currentId).ToList();
       var productGroup = new ProductGroup(Guid.NewGuid(), currentProductId);

      for (int j = i + 1; j < ids.Count; j++)
      {
         var subsequentId = ids[j];
         var subsequentEntity = tableResult.Where(x => x.id == subsequentId ).ToList();
         //This is my extension method which does the comparison
         if(currentEntity.EqualsAll(subsequentEntity))
         {
            productGroup.Id.Add(subsequentId );
            //am removing the product to avoid extra loop
            ids.RemoveAt(j);
            //adjust the index to match the removed product
            j = j - 1;
         }
      }
  }
  listOfProductGroup.Add(productGroup);


public class ProductGroup
{

    public ProductGroup(Guid groupId, int id)
    {
        GroupId= groupId;
        Id= new List<int>()
        {
            id
        };
    }
    public Guid GroupId{ get; set; }
    public IList<int> Id { get; set; }
}
var tableResult=\u repository.GetData().OrderBy(x=>x.NameId.ToList();
var id=tableResult.Select(x=>x.id).Distinct().ToList();
var listOfProductGroup=新列表();
for(int i=0;ix.id==currentId.ToList();
var productGroup=新产品组(Guid.NewGuid(),currentProductId);
对于(int j=i+1;jx.id==subsequentId.ToList();
//这是我进行比较的扩展方法
if(currentEntity.EqualsAll(子序列))
{
productGroup.Id.Add(后续TID);
//我正在移除产品以避免额外的循环
id.RemoveAt(j);
//调整索引以匹配移除的产品
j=j-1;
}
}
}
产品组列表。添加(产品组);
公共类产品组
{
公共产品组(Guid groupId,int id)
{
GroupId=GroupId;
Id=新列表()
{
身份证件
};
}
公共Guid GroupId{get;set;}
公共IList Id{get;set;}
}

类似这样的事情,也许:

class DictionaryComparer : IEqualityComparer<Dictionary<int, int>>
{
    public bool Equals(Dictionary<int, int> x, Dictionary<int, int> y)
    {
        return x.Count == y.Count && !x.Except(y).Any();
    }

    public int GetHashCode(Dictionary<int, int> obj)
    {
        int hash = 0;
        foreach (var kvp in obj.OrderBy(x => x.Key))
        {
            hash = hash ^ EqualityComparer<KeyValuePair<int, int>>.Default.GetHashCode(kvp);
        }
        return hash;
    }
}
class Thing
{
    public int Id { get; set; }
    public int NameId { get; set; }
    public int ValueId { get; set; }

    public Thing(int id, int nameId, int valueId)
    {
        Id = id;
        NameId = nameId;
        ValueId = valueId;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var data = new Thing[]
        {
            new Thing(1,       1,       4),
            new Thing(1,       10,      18),
            new Thing(1,       9,       15),
            new Thing(2,       1,       4),
            new Thing(2,       10,      17),
            new Thing(2,       9,       0),
            new Thing(3,       1,       5),
            new Thing(3,       9,       16),
            new Thing(3,       10,      18),
            new Thing(4,       1,       5),
            new Thing(4,       10,      18),
            new Thing(4,       9,       16),
            new Thing(5,       1,       4),
            new Thing(5,       10,      17),
            new Thing(5,       9,       0),
        };

        var @as = data.GroupBy(x => x.Id)
            .Select(x => new {Id = x.Key, Data = x.ToDictionary(t => t.NameId, t => t.ValueId)})
            .GroupBy(x => x.Data, x => x.Id, new DictionaryComparer());

        foreach (var a in @as)
        {
            Console.WriteLine("{0} {1}", string.Join(",", a), string.Join(",", a.Key.Values));
        }

        Console.ReadKey();
    }
}
类字典比较程序:IEqualityComparer
{
公共布尔等于(字典x、字典y)
{
返回x.Count==y.Count&&!x.Except(y).Any();
}
公共int GetHashCode(字典obj)
{
int hash=0;
foreach(obj.OrderBy中的var kvp(x=>x.Key))
{
hash=hash^EqualityComparer.Default.GetHashCode(kvp);
}
返回散列;
}
}
阶级事务
{
公共int Id{get;set;}
公共int NameId{get;set;}
public int ValueId{get;set;}
公共事物(int-id、int-nameId、int-valueId)
{
Id=Id;
NameId=NameId;
ValueId=ValueId;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
var数据=新事物[]
{
新事物(1,1,4),
新事物(1,10,18),
新事物(1,9,15),
新事物(2,1,4),
新事物(2,10,17),
新事物(2,9,0),
新事物(3,1,5),
新事物(3,9,16),
新事物(3,10,18),
新事物(4,1,5),
新事物(4,10,18),
新事物(4,9,16),
新事物(5,1,4),
新事物(5,10,17),
新事物(5,9,0),
};
var@as=data.GroupBy(x=>x.Id)
.Select(x=>new{Id=x.Key,Data=x.ToDictionary(t=>t.NameId,t=>t.ValueId)})
.GroupBy(x=>x.Data,x=>x.Id,新字典comparer());
foreach(变量a在@as中)
{
Console.WriteLine(“{0}{1}”、string.Join(“,”,a)、string.Join(“,”,a.Key.Values));
}
Console.ReadKey();
}
}

类似这样的事情,也许:

class DictionaryComparer : IEqualityComparer<Dictionary<int, int>>
{
    public bool Equals(Dictionary<int, int> x, Dictionary<int, int> y)
    {
        return x.Count == y.Count && !x.Except(y).Any();
    }

    public int GetHashCode(Dictionary<int, int> obj)
    {
        int hash = 0;
        foreach (var kvp in obj.OrderBy(x => x.Key))
        {
            hash = hash ^ EqualityComparer<KeyValuePair<int, int>>.Default.GetHashCode(kvp);
        }
        return hash;
    }
}
class Thing
{
    public int Id { get; set; }
    public int NameId { get; set; }
    public int ValueId { get; set; }

    public Thing(int id, int nameId, int valueId)
    {
        Id = id;
        NameId = nameId;
        ValueId = valueId;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var data = new Thing[]
        {
            new Thing(1,       1,       4),
            new Thing(1,       10,      18),
            new Thing(1,       9,       15),
            new Thing(2,       1,       4),
            new Thing(2,       10,      17),
            new Thing(2,       9,       0),
            new Thing(3,       1,       5),
            new Thing(3,       9,       16),
            new Thing(3,       10,      18),
            new Thing(4,       1,       5),
            new Thing(4,       10,      18),
            new Thing(4,       9,       16),
            new Thing(5,       1,       4),
            new Thing(5,       10,      17),
            new Thing(5,       9,       0),
        };

        var @as = data.GroupBy(x => x.Id)
            .Select(x => new {Id = x.Key, Data = x.ToDictionary(t => t.NameId, t => t.ValueId)})
            .GroupBy(x => x.Data, x => x.Id, new DictionaryComparer());

        foreach (var a in @as)
        {
            Console.WriteLine("{0} {1}", string.Join(",", a), string.Join(",", a.Key.Values));
        }

        Console.ReadKey();
    }
}
类字典比较程序:IEqualityComparer
{
公共布尔等于(字典x、字典y)
{
返回x.Count==y.Count&&!x.Except(y).Any();
}
公共int GetHashCode(字典obj)
{
int hash=0;
foreach(obj.OrderBy中的var kvp(x=>x.Key))
{
hash=hash^EqualityComparer.Default.GetHashCode(kvp);
}
返回散列;
}
}
阶级事务
{
公共int Id{get;set;}
公共int NameId{get;set;}
public int ValueId{get;set;}
公共事物(int-id、int-nameId、int-valueId)
{
Id=Id;
NameId=NameId;
ValueId=ValueId;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
var数据=新事物[]
{
新事物(1,1,4),
新事物(1,10,18),
新事物(1,9,15),
新事物(2,1,4),
新事物(2,10,17),
新事物(2,9,0),
新事物(3,1,5),
新事物(3,9,16),
新事物(3,10,18),
新事物(4,1,5),
新事物(4,10,18),
新事物(4,9,16),
新事物(5,1,4),
新事物(5,10,17),
新事物(5,9,0),
};
var@as=data.GroupBy(x=>x.Id)
.Select(x=>new{Id=x.Key,Data=x.ToDictionary(t=>t.NameId,t=>t.ValueId)})
.GroupBy(x=>x.Data,x=>x.Id,新Dic