【问题标题】:searches a name from list of different types从不同类型的列表中搜索名称
【发布时间】:2020-10-10 17:10:27
【问题描述】:

我尝试了以下一些逻辑,但我正在寻找更通用的解决方案,请帮助我是否有其他方法可以获取对象。

static class FinObj
{
    public static ppl Find(List<ppl> obj, string Name)
    {
        foreach (var item in obj)
        {              
            if (item.Name == Name)
            { 
                return (item);
            }
        }
        return null;
    }
}
    
static void Main(string[] args)
{
      
    student d = FinObj.Find(stuList, "B") as student;

    teacher x = FinObj.Find(patientList, "BB") as teacher; 
}

【问题讨论】:

    标签: c# .net list object generic-programming


    【解决方案1】:
     public static dynamic Find(List<Person> obj, string Name)
            {
                var result = obj.Find(x => x.Name == Name);
    
                if (result != null)
                {
                    Console.WriteLine("ID:" + result.ID + ", Name:" + result.Name + ", Gender:" + result.Gender);
                }
                Console.WriteLine("Not Found");
                return result;
    
            }
    

    【讨论】:

      【解决方案2】:

      您可以自己使用泛型。这样,您曾经声明的方法将自动返回正确的类:

      using System;
      using System.Collections.Generic;
      using System.Linq; 
      
      class Person
      {
          public int ID { get; set; }
          public string Name { get; set; }
          public char Gender { get; set; }
      
          public override string ToString() => $"{GetType()}  {ID}  {Name}  {Gender}"; 
      }
      
      class Patient : Person { } 
      class Doctor : Person { }
      
      static class FinderObject
      {
          // what you want to do can be done using Linq on a list with FirstOrDefault
          // so there is no real use to create the method yourself if you could instead
          // leverage linq
          public static T Find<T>(List<T> data, string Name)
              where T : Person 
              => data.FirstOrDefault(p => p.Name == Name);
      }
      

      用法:

      public static class Program
      {
          static void Main(string[] args)
          {
              var docList = Enumerable.Range(1, 10)
                  .Select(n => new Doctor() { ID = n, Name = $"Name{n}", Gender = 'D' })
                  .ToList();
      
              var patientList = Enumerable.Range(100, 110)
                  .Select(n => new Patient() { ID = n, Name = $"Name{n}", Gender = 'D' })
                  .ToList(); 
      
              // finds a doctor by that name
              Doctor d = FinderObject.Find(docList, "Name3");
      
              // patient does not exist
              Patient x = FinderObject.Find(patientList, "Name99");
      
              Console.WriteLine($"Doc: {d}");
              Console.WriteLine($"Pat: {(x != null ? x.ToString() : "No such patient")}");
              Console.ReadLine();
          } 
      }
      

      输出:

      Doc: Doctor  3  Name3 D
      Pat: No such patient
      

      见:

      【讨论】:

      • 非常感谢您的回答,我已使用动态关键字作为返回类型以避免类型转换。
      猜你喜欢
      • 2014-09-17
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      相关资源
      最近更新 更多