本文介绍了如何实现采用Collection&lt; Feature&gt;的方法作为参数,将返回Collection&lt; Feature&gt;这些是唯一的CustID和年龄<35的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 课程专题     {         public int customerID {get;组; }         public String name {get;组; }         public int age {get;组; }     } class Feature    {        public int customerID { get; set; }        public String name { get; set; }        public int age { get; set; }    } 我想 实施一种方法集合< 功能 > 作为参数,将返回Collection< 功能 > 所有 功能 是唯一的" ; customerID " 和年龄< 35. I would like toimplement a method taking Collection<Feature> as an argument and will return Collection<Feature> where all Features are unique by "customerID" and age < 35. 如果上述要求不符合那么我想 将任何输入元素放入目标集合中。然后打印所有这些。  If the above requirements is not match then I want toput in target collection any of input elements. Then print all of them.  LINQ具有惊人的容量,或许可以找到独特的 customerID 和年龄 但我不是确定我如何实现它。  LINQ has amazing capacity that perhaps can find uniquecustomerIDand age < 35 through Distinct() and join,but I am not sure how I can implement it. 可以在上面的方法中使用LINQ来完成返回集合<功能>?  Can it be done using LINQ inside the above method that will return Collection<Feature>? 您对代码示例的帮助以实现上述方法将是巨大的。  Your assistance with code sample to implement the above method will be monumental. 这是我的代码:namespace FeatureCustomer{ class Feature { public int custid { get; set; } public String name { get; set; } public int age { get; set; } } class Program { static void Main(string[] args) { List<Feature> customers = new List<Feature>(); customers.Add(new Feature { custid = 12, name = "George Abbott", age = 32 }); customers.Add(new Feature() { custid = 10, name = "Liz Duffy Adams", age = 32 }); customers.Add(new Feature() { custid = 15, name = "Douglass Adair", age = 45 }); customers.Add(new Feature() { custid = 1, name = "Joan Abelove", age = 22 }); customers.Add(new Feature() { custid = 1, name = "Paul Ackerman", age = 22 }); customers.Add(new Feature() { custid = 11, name = "Tania McCormick", age = 34 }); foreach (Feature p in customers) System.Console.WriteLine(p.name); var groupedCustList = customers.GroupBy(u => u.age) .Select(grp => grp.ToList()).ToList(); foreach (var group in groupedCustList) { Console.WriteLine(String.Join(" ", group)); foreach (var user in group) { Console.WriteLine(" {0}", user.name + " " + user.age); } } } }}当我运行上面的代码时以下行将是控制台输出:When I run the above code the following lines will be console output: George Abbott Liz Duffy Adams Douglass Adair Joan Abelove Paul Ackerman Tania McCormick   FeaturePerson.Feature FeaturePerson.Feature   George Abbott 32  George Abbott 32   Liz Duffy Adams 32  Liz Duffy Adams 32 FeaturePerson.Feature   Douglass Adair 45  Douglass Adair 45 FeaturePerson.Feature FeaturePerson.Feature   Joan Abelove 22  Joan Abelove 22   Paul Ackerman 22  Paul Ackerman 22 FeaturePerson.Feature   Tania McCormick 34  Tania McCormick 34 我尝试实现一个LINQ查询,但它不是我想要的,为什么我得到以下行? I try to implement a LINQ query but its not what I want and why I get the following line? FeaturePerson.Feature FeaturePerson.Feature。 因此,一旦实施该方法,程序应在控制台上打印以下内容:  So once the method is implemented the program should print on console the followings:  唯一ID和年龄The unique ID and age < 35 Customers are ID: 12,姓名:George Abbott,年龄:​​32 ID: 12, name: George Abbott, age: 32 ID: 12,姓名: Liz Duffy Adams , 年龄:32 ID: 12,名称: Joan Abelove , 年龄:32等。 无与伦比的客户 custid = 15,name =" Douglass Adair",age = 45 custid = 1,name =" Paul Ackerman",age = 22   推荐答案 您好,  您错过了对查询的评估;  You are missing the evaluation of the query;   更改以下代码 Change the following code (u => u.age) Console.WriteLine(" {0}", user.name + " " + user.age);  To (u => u.age < 35 ) Console.WriteLine( string.Format( " {0}: {1} {2} ", user.custid, user.name, user.age ) );  希望这会有所帮助:) Hope this helps :) 这篇关于如何实现采用Collection&lt; Feature&gt;的方法作为参数,将返回Collection&lt; Feature&gt;这些是唯一的CustID和年龄<35的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 23:01
查看更多