【问题标题】:How to return array from database in c#如何在c#中从数据库中返回数组
【发布时间】:2023-03-17 06:41:08
【问题描述】:

我想将我的结果作为数组列表返回。我的代码如下所示:

public Person Get(string doctorCode)
{
     using (PharmaOCEAN_LTEntities entities = new PharmaOCEAN_LTEntities())                   
     {
         return entities.Person.FirstOrDefault(e => e.DoctorLicenseNumber == doctorCode);
     }
}

有人告诉我选择的(全部)结果将是一个数组,所以我尝试了这种方式,但我在选择语句时遇到错误:

public IList<Person> Get(string doctorCode)
{
    using (PharmaOCEAN_LTEntities entities = new PharmaOCEAN_LTEntities())                   
    {
        return entities.Person.Select<Person>(e => e.DoctorLicenseNumber == doctorCode);
    }
}

有什么意见吗?

【问题讨论】:

    标签: c# asp.net arrays


    【解决方案1】:

    当然可以! 作为一个数组:

    public Person[] Get(string doctorCode)
    {
        using (PharmaOCEAN_LTEntities entities = new PharmaOCEAN_LTEntities())                   
        {
            return entities.Person.Where(e => e.DoctorLicenseNumber == doctorCode).ToArray();
        }
    }
    

    作为一个列表:

    public IEnumerable<Person> Get(string doctorCode)
    {
        using (PharmaOCEAN_LTEntities entities = new PharmaOCEAN_LTEntities())                   
        {
            return entities.Person.Where(e => e.DoctorLicenseNumber == doctorCode).ToList();
        }
    }
    

    不确定它是否编译,但你会收到消息:)

    【讨论】:

      【解决方案2】:

      你能提供你得到的信息错误吗?

      你不必在select语句的末尾添加ToList()吗?

      【讨论】:

        猜你喜欢
        • 2015-12-05
        • 1970-01-01
        • 1970-01-01
        • 2019-11-14
        • 2020-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多