猿问

C# 查询键值列表并查找重复项

我在 c# 中有一个列表:


List<Data> uData = new List<uData>();

其中 uData 从 UI 填充为:


{

   Id: 1,

   Name: "Smith",

   Input: "7,8",

   Output: "Output1",

   CreatedBy: "swallac",

   CreatedON: "12/01/2018"

},

{

   Id: 2,

   Name: "Austin",

   Input: "7,8",

   Output: "Output1",

   CreatedBy: "awanda",

   CreatedON: "12/03/2018"

},

{

   Id: 1,

   Name: "Smith",

   Input: "22,22",

   Output: "Output2",

   CreatedBy: "swallac",

   CreatedON: "12/01/2018"

},

{

   Id: 1,

   Name: "Smith",

   Input: "9,8",

   Output: "Output2",

   CreatedBy: "swallac",

   CreatedON: "12/01/2018"

},

{

   Id: 1,

   Name: "Peter",

   Input: "1,10",

   Output: "Output3",

   CreatedBy: "pjon",

   CreatedON: "12/02/2018"

}

我想要做的是在“输出”键上搜索这个列表,并找出“输入”键的相应值是否有重复项。


例如,在我上面的示例列表中,我有三个输出:Output1、Output2、Output3。现在对于输出值为“Output1”的键的列表,相应的“输入”键在此处重复。值为“7,8”。这是我要强调的。所以 Output1 具有 Output2 和 Output3 没有的重复输入。


我可以像下面这样首先找出输出然后检查值:


var myList = uData.Where(p => p.Output == "Output").First();

但我不会提前知道所有的输出来检查。


有什么可以开始的输入吗?


回首忆惘然
浏览 265回答 3
3回答

莫回无

您可以获得重复项的列表,如下所示:var&nbsp;myList&nbsp;=&nbsp;uData.GroupBy(l&nbsp;=>&nbsp;l.Ouput) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(g&nbsp;=>&nbsp;g.GroupBy(x&nbsp;=>&nbsp;x.Input).Where(x&nbsp;=>&nbsp;x.Count()&nbsp;>&nbsp;1)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SelectMany(x&nbsp;=>&nbsp;x);按输出分组以获得一系列相关项目Output用于SelectMany扁平化按输入分组的结果,其中有重复项用于SelectMany展平为单个IEnumerable<Data>否则,如果您只想知道是否有基于上述标准的重复项,那么您可以使用Anyvar&nbsp;anyDuplicates&nbsp;=&nbsp;uData.GroupBy(l&nbsp;=>&nbsp;l.Ouput) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Any(g&nbsp;=>&nbsp;g.GroupBy(x&nbsp;=>&nbsp;x.Input).Any(x&nbsp;=>&nbsp;x.Count()&nbsp;>&nbsp;1));

慕容3067478

如果我正确理解您的问题,您不想搜索特定输出,而是查找所有重复的输出和输入值。您可以通过对组合进行分组并过滤具有多个条目的组来做到这一点:var&nbsp;duplicates&nbsp;=&nbsp;uData.GroupBy(d=>new{d.Input,d.Output}).Where(g=>g.Count()&nbsp;>&nbsp;1)对于您的示例,上面返回(可枚举)一个组,其键为 {Output: "Output1", Input: "7,8}。(该组本身包含具有该组合的所有元素)

慕勒3428872

您可以使用查找,这将创建一个对象,您可以使用您指定的键进行迭代:public class Program{&nbsp; &nbsp; public static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<data> myList = new List<data>();&nbsp; &nbsp; &nbsp; &nbsp; myList.Add(new data("output1", "1,5"));&nbsp; &nbsp; &nbsp; &nbsp; myList.Add(new data("output2", "1,6"));&nbsp; &nbsp; &nbsp; &nbsp; myList.Add(new data("output1", "1,5"));&nbsp; &nbsp; &nbsp; &nbsp; myList.Add(new data("output1", "2,0"));&nbsp; &nbsp; &nbsp; &nbsp; ILookup<string, string> myLookup = myList.ToLookup(d => d.output, d => d.input);&nbsp; &nbsp; &nbsp; &nbsp; foreach (IGrouping<string, string> items in myLookup)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Output : " + items.Key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (string input in items)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("-- Input value is " + input);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public class data{&nbsp; &nbsp; public string output;&nbsp; &nbsp; public string input;&nbsp; &nbsp; public data(string output, string input)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.output = output;&nbsp; &nbsp; &nbsp; &nbsp; this.input = input;&nbsp; &nbsp; }}输出将如下所示:输出 : output1-- 输入值为 1,5-- 输入值为 1,5-- 输入值为 2,0输出 : output2-- 输入值为 1,6然后您可以检查任何两个输入值是否相同。
随时随地看视频慕课网APP
我要回答