【问题标题】:Finding the data of object type of list by giving its index value [closed]通过给出其索引值来查找列表对象类型的数据[关闭]
【发布时间】:2020-01-28 08:36:35
【问题描述】:

我有这样的课程:

    public class UsersTest
    {
        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string SecondName { get; set; }
    }

我想为我的 API 控制器提供一个参数。


        public List<UsersTest> users = new List<UsersTest>
        {
            new UsersTest{ UserID=1, FirstName="Kamal", SecondName="Seferov"},
            new UsersTest{ UserID=2, FirstName="Orxan", SecondName="Esedov"},
            new UsersTest{ UserID=3, FirstName="Cosqun", SecondName="Isayev"},
            new UsersTest{ UserID=4, FirstName="Elvin", SecondName="Hasanov"}
        };
        public List<UsersTest> Get(int index)
        {
            return users [index]; //error
        }

所以当我将参数设置为 3 时,可以说它应该显示列表的第三个索引

【问题讨论】:

  • //error - 想详细说明一下吗?
  • 我想显示列表的第三个索引
  • ...错误是?
  • 你为什么不用users.Where(x=&gt;x.UserId = index)
  • index 是指索引还是UserID

标签: c# list api


【解决方案1】:

您的函数应该返回UserTest 对象,而不是List

public UsersTest Get(int index)
{
    return users[index];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 2020-12-17
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多