本文介绍了帮助在c#.net中搜索meathod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我尝试了所有方法find,findAll,contains,exits但是seachbyFirstName,searchbyLastName根本不工作!! plz有人能帮忙吗? SearchbyFirstName和其他人的构造函数在地址簿类中!! 我尝试过: 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 使用System.Threading.Tasks; 命名空间ConsoleApplication14 { 公共类Class1 { private List< class2>人员; public Class1() { persons = new List< class2>(); } public virtual bool add(Class2 person) { foreach(Class2 p in persons) { if(string.Compare(p.Id,ID,StringComparison.OrdinalIgnoreCase)== 0) { 返回false; } } 人。添加(人); 返回true; } public virtual bool delete(Class2 person) { for(int i = 0; i< persons.Count; i ++ ) { Class2 p = people [i]; if(p.Equals(person)) { persons.RemoveAt(i); 返回真的; } } 返回false; } 公共虚拟列表与LT;等级2> searchByFirstName(string fName) { List< class2> res = new List< class2>(); for(int i = 0; i< persons.Count; i ++) { Class2 p = people [i]; if(p。[i] .StringComparison.OrdinalIgnoreCase(fName)== 0) { res.Add(p); } } 返回res; } 公共虚拟列表< class2> searchByLastName(string lName) { List< class2> res = new List< class2>(); for(int i = 0; i< persons.Count; i ++) { Class2 p = persons [i]; if(string.Compare(p.LastName,LastName,StringComparison.OrdinalIgnoreCase)== 0) { res.Add(p); } } 返回res; } 公共虚拟Class2 searchById(字符串id) { for(int i = 0; i< persons.Count; i ++ ) { Class2 p = people [i]; if(String.Compare(p.Id,ID,StringComparison。 OrdinalIgnoreCase)== 0) { 返回p; } } 返回null; } public v irtual void print() { Console.WriteLine(打印地址簿); foreach(Class2 p in人) { Console.WriteLine(p); } } } } PersonClass 使用System; 使用System.Linq; 使用System.Text; 使用System.Threading.Tasks; 命名空间ConsoleApplication14 { 公共类Class2 { private string firstName; private string lastName; private string id; public Class2 (string firstName,string lastName,string id):base() { this.firstName = firstName; this.lastName = lastName; this.id = id; } 公共虚拟字符串FirstName { get { 返回firstName; } set { this.firstName = FirstName; } } $ / $ public virtual string LastName { get { return lastName ; } set { this.lastName = LastName; } } 公共虚拟字符串ID { get { 返回ID; } 设置 { this.id = Id ; } } 公共覆盖字符串ToString() { 返回Person [firstName =+ FirstName +,lastName =+ LastName +,id =+ Id +]; } } } addressBookTester 使用System; 使用System.Linq; 使用System.Text; 使用System.Threading.Tasks; 命名空间ConsoleApplication14 { class program { static void Main(string [] args) { Console.WriteLine(创建AddressBook类); Class1 addressBook = new Class1(); Class2 p1 = new Class2(fName1 ,lName3,x0001); Class2 p2 = new Class2(fName2,lName4,x0002); Class2 p3 = new Class2 (fName3,lName9,x0003); Class2 p4 = new Class2(fName4,lName8,x0004); Class2 p5 = new Class2(fName5,lName6,x0005); Class2 p6 = new Class2(fName6,lName4,x0006); Class2 p7 = new Class2(fName7,lName1,x0007); Class2 p8 = new Class2(fName8,lName4,x0008); Class2 p9 = new Class2(fName9,lName7,x0009); Class2 p10 = new Cl ass2(fName10,lName6,x00010); Class2 p11 = new Class2(fName1,lName2,x00011); Class2 p12 = new Class2(fName2,lName3,x0001); Console.WriteLine(Expected:true \t+ p1 +addition:+ addressBook.add (p1)); Console.WriteLine(预期:true \t+ p2 +addition:+ addressBook.add(p2)); 控制台.WriteLine(Expected:true \t+ p3 +addition:+ addressBook.add(p3)); Console.WriteLine(Expected:true \t+ p4 + 另外:+ addressBook.add(p4)); Console.WriteLine(Expected:true \t+ p5 +addition:+ addressBook.add(p5)); Console.WriteLine(Expected:true \t+ p6 +addition:+ addressBook.add(p6)); Console.WriteLine(预期: true \t+ p7 +另外:+ addressBook.add(p7)); Console.WriteLine(预期:true \t+ p8 +另外:+ addressBook .add(p8)); Console.WriteLine(Expected:true \t+ p9 +addition:+ addressBook.add(p9)); Console.WriteLine(Expected:true \t+ p10 +addition:+ addressBook.add(p10)); Console.WriteLine(Expected:false \t+ p11 +addition:+ addressBook.add(p11)); Console.WriteLine(Expected:false \t+ p12 +addition:+ addressBook.add(p12)); Console.WriteLine(Before删除人); addressBook.print(); Console.WriteLine(删除Id x0007的人); addressBook。删除(p7); Console.WriteLine(删除人员后); addressBook.print(); Console.WriteLine( 使用Id x0001搜索人员; Console.WriteLine(结果:+ addressBook.searchById(x0001)); Console.WriteLine(搜索名字为fName1的人); Console.WriteLine(结果:+ addressBook.searchByFirstName(fname1)); Console.WriteLine(搜索姓氏为lName4的人); Console.WriteLine(结果:+ addressBook.searchByLastName(lname4)); } } } I tried with all the methods find, findAll, contains, exits but the seachbyFirstName,searchbyLastName is not working at all!!plz can someone help??The Constructor for SearchbyFirstName and others are in addressbook class!!What I have tried:AddressBook classusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication14{ public class Class1 { private List<class2> persons; public Class1(){persons = new List<class2>();} public virtual bool add(Class2 person) { foreach (Class2 p in persons) { if (string.Compare(p.Id, " ID ", StringComparison.OrdinalIgnoreCase) == 0) { return false; } } persons.Add(person); return true; } public virtual bool delete(Class2 person) { for (int i = 0; i < persons.Count; i++) { Class2 p = persons[i]; if (p.Equals(person)) { persons.RemoveAt(i); return true; } } return false; } public virtual List<class2> searchByFirstName(string fName) { List<class2> res = new List<class2>(); for (int i = 0; i < persons.Count; i++) { Class2 p = persons[i]; if( p.[i].StringComparison.OrdinalIgnoreCase(fName)==0 ) { res.Add(p); } } return res; } public virtual List<class2> searchByLastName(string lName) { List<class2> res = new List <class2>(); for (int i = 0; i < persons.Count; i++) { Class2 p = persons[i]; if (string.Compare(p.LastName,"LastName", StringComparison.OrdinalIgnoreCase) == 0) { res.Add(p); } } return res; } public virtual Class2 searchById(string id) { for (int i = 0; i < persons.Count; i++) { Class2 p = persons[i]; if (String.Compare(p.Id, "ID", StringComparison.OrdinalIgnoreCase) == 0) { return p; } } return null; } public virtual void print() { Console.WriteLine("Printing Address Book"); foreach (Class2 p in persons) { Console.WriteLine(p); } } }}PersonClassusing System;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication14{ public class Class2 { private string firstName;private string lastName;private string id;public Class2 (string firstName, string lastName, string id):base() {this.firstName = firstName;this.lastName = lastName;this.id = id;}public virtual string FirstName{get{return firstName;}set{this.firstName = FirstName;}}public virtual string LastName{get{return lastName;}set{this.lastName = LastName;}}public virtual string Id{get{return id;}set{this.id = Id;}}public override string ToString(){return "Person[firstName=" + FirstName + ", lastName=" + LastName + ", id=" + Id + "]";} }}addressBookTesterusing System;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication14{ class Program { static void Main(string[] args) { Console.WriteLine("Creating AddressBook Class");Class1 addressBook = new Class1();Class2 p1 = new Class2("fName1", "lName3", "x0001");Class2 p2 = new Class2("fName2", "lName4", "x0002");Class2 p3 = new Class2("fName3", "lName9", "x0003");Class2 p4 = new Class2("fName4", "lName8", "x0004");Class2 p5 = new Class2("fName5", "lName6", "x0005");Class2 p6 = new Class2("fName6", "lName4", "x0006");Class2 p7 = new Class2("fName7", "lName1", "x0007");Class2 p8 = new Class2("fName8", "lName4", "x0008");Class2 p9 = new Class2("fName9", "lName7", "x0009");Class2 p10 = new Class2("fName10", "lName6", "x00010");Class2 p11 = new Class2("fName1", "lName2", "x00011");Class2 p12 = new Class2("fName2", "lName3", "x0001");Console.WriteLine("Expected : true \t" + p1 + " addition :" + addressBook.add(p1));Console.WriteLine("Expected : true \t" + p2 + " addition :" + addressBook.add(p2));Console.WriteLine("Expected : true \t" + p3 + " addition :" + addressBook.add(p3));Console.WriteLine("Expected : true \t" + p4 + " addition :" + addressBook.add(p4));Console.WriteLine("Expected : true \t" + p5 + " addition :" + addressBook.add(p5));Console.WriteLine("Expected : true \t" + p6 + " addition :" + addressBook.add(p6));Console.WriteLine("Expected : true \t" + p7 + " addition :" + addressBook.add(p7));Console.WriteLine("Expected : true \t" + p8 + " addition :" + addressBook.add(p8));Console.WriteLine("Expected : true \t" + p9 + " addition :" + addressBook.add(p9));Console.WriteLine("Expected : true \t" + p10 + " addition :" + addressBook.add(p10));Console.WriteLine("Expected : false \t" + p11 + " addition :" + addressBook.add(p11));Console.WriteLine("Expected : false \t" + p12 + " addition :" + addressBook.add(p12));Console.WriteLine("Before deleting Person");addressBook.print();Console.WriteLine("Deleting Person with Id x0007");addressBook.delete(p7);Console.WriteLine("After deleting Person");addressBook.print();Console.WriteLine("Searching Persons with Id x0001");Console.WriteLine("Result :" + addressBook.searchById("x0001"));Console.WriteLine("Searching Persons with First Name fName1");Console.WriteLine("Result :" + addressBook.searchByFirstName("fname1"));Console.WriteLine("Searching Persons with Last Name lName4");Console.WriteLine("Result :" + addressBook.searchByLastName("lname4")); } }}推荐答案 searchByLastName 永远不会起作用,除非您的用户被命名为John LastName - 因为您与固定字符串进行比较: searchByLastName is never going to work, unless your user is named "John LastName" - because you compare with a fixed string:public virtual List<class2> searchByLastName(string lName) { List<class2> res = new List <class2>(); for (int i = 0; i < persons.Count; i++) { Class2 p = persons[i]; if (string.Compare(p.LastName,"LastName", StringComparison.OrdinalIgnoreCase) == 0) { res.Add(p); } } return res; } searchByFirstName 无效,因为它甚至无法编译! searchByFirstName won't work because it won't even compile!public virtual List<class2> searchByFirstName(string fName) { List<class2> res = new List<class2>(); for (int i = 0; i < persons.Count; i++) { Class2 p = persons[i]; if( p.[i].StringComparison.OrdinalIgnoreCase(fName)==0 ) { res.Add(p); } } }因为你没有指定要比较的Class2实例的哪个部分,所以你只需用点和方括号做一些奇怪的事情。 /> 请自己帮个忙:目前你似乎试图通过猜测来编码,从长远来看,这不是一个可行的持续战略。现在停下来,开始思考你想要做什么,以及你引入的代码元素。然后尝试一次完成一项工作,使其工作并进行全面测试,然后继续执行下一部分任务。通过名字和姓氏搜索应该是几乎相同的方法,所以让一个工作,它会给你另一个作为免费赠品!Because you don't specify what part of your Class2 instance to compare, you just do something odd with dot and square brackets.Please, do yourself a favour: at the moment you appear to be trying to code by guesswork, and that isn't a viable ongoing strategy in the long term. Stop now, and start thinking about what you are trying to do, and the code elements you have been introduced to. Then try doing one job at a time, get that working and thoroughly tested, then move on to the next part of the task. Get search by first name and by last name should be almost identical methods, so get one working and it will give you the other as a freebie! 这篇关于帮助在c#.net中搜索meathod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 02:48