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# 获取有两条记录的元素列表_C#_Linq - Fatal编程技术网

C# 获取有两条记录的元素列表

C# 获取有两条记录的元素列表,c#,linq,C#,Linq,我有一张看起来有点像这样的桌子 id - int tpc - string dcid - int other fields 在这个tpc和dcid中唯一的一对tpc此时可以是LQ或GR。可能还会有更多,但目前只有两个 所以我想通过LINQ获得一个记录列表,其中有两个记录具有相同的dcid,一个用于LQ,一个用于GR 因此,如果我有以下数据 1, GR, 1, ... 2, LQ, 1, ... 3, GR, 2, ... 4, GR, 3, ... 5, LQ, 3, ... 我希望返回的列

我有一张看起来有点像这样的桌子

id - int
tpc - string
dcid - int
other fields
在这个
tpc
dcid
中唯一的一对<代码>tpc此时可以是LQ或GR。可能还会有更多,但目前只有两个

所以我想通过LINQ获得一个记录列表,其中有两个记录具有相同的dcid,一个用于LQ,一个用于GR

因此,如果我有以下数据

1, GR, 1, ...
2, LQ, 1, ...
3, GR, 2, ...
4, GR, 3, ...
5, LQ, 3, ...
我希望返回的列表是1,2,4,5。
3将不在列表中,因为它缺少dcid 2的LQ条目


我尝试过各种查询,但在逻辑上很难用tpc检查两个dcid。

因此,如果我理解正确,您希望返回每个可用tpc类型的记录所在的ID。在您的示例中,有2种TPC类型,但可能还有更多

类似这样的工作原理:

var tpcs = new List<string>();

var filtered = records.GroupBy(record =>
{
  //Keep a track of the available TPC types
  if (!tpcs.Contains(record.TPC))
  {
    tpcs.Add(record.TPC);
  }

  return record.DCID;

})
.Where(group =>
{
  //Get the TPCs for the group
  var groupTpcs = group.Select(record => record.TPC);

  //Only return groups that have records for all the TPC types
  return groupTpcs.Intersect(tpcs).Count() == tpcs.Count;       
});

//Print groups
foreach (var group in filtered)
{
  Console.WriteLine(group.Key);
  foreach (var record in group)
  {
    Console.WriteLine("\tID = {0}, TPC = {1}, DCID = {2}", record.ID, record.TPC, record.DCID);
  }
}
如果您只需要ID,请在LINQ语句的末尾添加SelectMany

.SelectMany(group =>
{
  //Return a list of IDs for each record in the group
  return group.Select(record => record.ID);
});

但我不确定是否推荐这种方法。

因此,如果我理解正确,您希望返回每个可用TPC类型的记录所在的ID。在您的示例中,有2种TPC类型,但可能还有更多

var required = new List<string> { "LQ", "GR" };
var query = data
    .GroupBy(x => x.dcid)
    .Where(g => g
        .Select(x => x.tpc)
        .Intersect(required)
        .Count() == required.Count)
    .Select(g => g.Key);
类似这样的工作原理:

var tpcs = new List<string>();

var filtered = records.GroupBy(record =>
{
  //Keep a track of the available TPC types
  if (!tpcs.Contains(record.TPC))
  {
    tpcs.Add(record.TPC);
  }

  return record.DCID;

})
.Where(group =>
{
  //Get the TPCs for the group
  var groupTpcs = group.Select(record => record.TPC);

  //Only return groups that have records for all the TPC types
  return groupTpcs.Intersect(tpcs).Count() == tpcs.Count;       
});

//Print groups
foreach (var group in filtered)
{
  Console.WriteLine(group.Key);
  foreach (var record in group)
  {
    Console.WriteLine("\tID = {0}, TPC = {1}, DCID = {2}", record.ID, record.TPC, record.DCID);
  }
}
如果您只需要ID,请在LINQ语句的末尾添加SelectMany

.SelectMany(group =>
{
  //Return a list of IDs for each record in the group
  return group.Select(record => record.ID);
});
但我不确定是否推荐这种方法。

var required=newlist{“LQ”,“GR”};
var required = new List<string> { "LQ", "GR" };
var query = data
    .GroupBy(x => x.dcid)
    .Where(g => g
        .Select(x => x.tpc)
        .Intersect(required)
        .Count() == required.Count)
    .Select(g => g.Key);
变量查询=数据 .GroupBy(x=>x.dcid) .其中(g=>g .选择(x=>x.tpc) .Intersect(必选) .Count()==必需的.Count) .选择(g=>g.Key);
var required=新列表{“LQ”、“GR”};
变量查询=数据
.GroupBy(x=>x.dcid)
.其中(g=>g
.选择(x=>x.tpc)
.Intersect(必选)
.Count()==必需的.Count)
.选择(g=>g.Key);

您是否在dcid上进行了分组,并检查了计数大于1的位置?我想到了这一点,但tpc可能会包括第三个选项,比如有一天,我不想再次访问。如果没有人响应解决方案,我自己也找不到合适的解决方案,你在dcid上做了GroupBy并检查计数>1的位置了吗?我想到了,但tpc可能会包括第三个选项,比如说有一天,我不想再次访问。如果没有人给出解决方案,而我自己也找不到合适的解决方案,我也可以