如何使用c#在方法中返回两个列表 [英] How to return two list in a method using c#

查看:92
本文介绍了如何使用c#在方法中返回两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,如

public List< patientlogin> Patientlog(字符串用户名,字符串密码)

{



}



我想从这个方法返回2个列表。我使用了类似元组和参考但没有用的东西

请给出一些建议来解决这个问题。



i想要返回,例如

public List< patientlogin> Patientlog(字符串用户名,字符串密码)

{

列表< patientlogin> obj1 =新列表< patientlogin>();

列表< patientlogin> ; obj2 =新列表< patientlogin>();



返回obj1,obj2;



}



先谢谢。

I have a method like
public List<patientlogin> Patientlog(string Username, string Password)
{

}

and i want to return 2 list from this method.I used something like tuples and reference but no use
please give some suggestions to fix this issue.

i want to return like
public List<patientlogin> Patientlog(string Username, string Password)
{
List<patientlogin>obj1=new List<patientlogin>();
List<patientlogin>obj2=new list<patientlogin>();

return obj1,obj2;

}

Thanks in Advance.

推荐答案

大家好,



很有机会展示我的解决方案,如果这不合适请告诉我你的意见。



Hi Guys,

It was great chance to present my solution, if this is not appropriate please let me know your comments.

public class StudentService
{
  public List<student> GetAllStudent(out List<course> ListOfCourse)
  {
    var ListAllStudent = dbContext.Students.ToList();
    var ListOfCourse = dbContext.Students.Courses.ToList();

    return ListAllStudent; 
  }
}

public class StudentController
{
  public ActionResult GetAll()
  {
    StudentService objStudentService = new StudentService(); 
    List<course> CourseList = null;
    List<student> ListStudents = objStudentService.GetAllStudent(out CourseList);
    // This is pointless. If you want a different name, just use it! 
    var ListCourses = CourseList;

  }
}





这样你就可以获得ListStudents和CourseList。



编辑MTH:有点清理和错字修复。



So in this way you'll get ListStudents and CourseList.

Edit MTH: a bit of cleanup and typo fixing.


你不能从方法中返回两个项目。所以你可以做的是,使用字典

见下面的例子。

Well you can't return two item from method. So what you can do is, Use Dictionary.
See example below.
Dictionary<string, List<string>> myLists = new Dictionary<string, List<string>>();
public void Patientlog(string Username, string Password)
{
    List1=new List<string>();
    List2=new List<string>();
     
    myLists.Add("list1", List1);
    myLists.Add("list2", List2);
 
}

// Access the list somewhere else in code
List<string> tempList = myLists["list1"];
List<string> tempList2 = myLists["list2"];



-KR


-KR


您不能返回两个列表,但可以将两个列表添加到一个列表中,然后返回(如果需要)。然后你会相应区分。
You can not return two list, but you can add both list into one and then return if it is require for you. & then you differentiate accordingly.


这篇关于如何使用c#在方法中返回两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆