C# 为了读取数据库而不是SqlDataReader,我应该在LINQ中使用什么?

C# 为了读取数据库而不是SqlDataReader,我应该在LINQ中使用什么?,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我第一次尝试使用LINQ,但不知道如何读取数据库。我肯定我在做一些完全愚蠢的事情,只是需要一点指导 year = year.Where(x => x.statementYear == response[i]).ToList(); SqlDataReader reader; while (reader.Read()) { } 已编辑的问题,包含更多信息: 这就是我一直在努力的工作。。如果我使用using语句,我也不需要con.open,对吗 using (SqlConnection

我第一次尝试使用LINQ,但不知道如何读取数据库。我肯定我在做一些完全愚蠢的事情,只是需要一点指导

year = year.Where(x => x.statementYear == response[i]).ToList();

SqlDataReader reader;

while (reader.Read())
{

}
已编辑的问题,包含更多信息:

这就是我一直在努力的工作。。如果我使用using语句,我也不需要con.open,对吗

using (SqlConnection con = new SqlConnection(".."))
{
    List<string> paths = new List<string>();

    // Open Connection
    con.Open();

    if (response != null)
    {
        for (var i = 0; i < response.Length; i++)
        {
            if (response != null)
            {
                using (var db = new db())
                {
                    List<ClientStatement_Inventory> years = new List<ClientStatement_Inventory>();

                    years = years.Where(x => x.statementYear == response[i]).ToList();

                    foreach (var year in years)
                    {
                        paths.Add(year.statementPath);
                    }
                }
            }
        }
    }
}

使用Linq时,使用SqlDataReader没有任何用处或意义

实际上,您没有展示太多的内容—但基本上,使用Linq,您应该有一个DbContext,它是您与数据库的连接,并且DbContext应该包含任意数量的DbSet—基本上表示数据库中的表。从这些DbSet中选择,然后返回一个列表,您只需对其进行迭代

大致如下:

-- select your customers matching a certain criteria
var customers = NorthwindDbContext.Customers.Where(x => x.statementYear == response[i]).ToList();

-- iterate over your customers
foreach (Customer c in customers)
{
     // do whatever with your "Customer" here
}
if (response != null)
{
    List<string> allPaths = new List<string>();

    using (var db = new FMBDBPRDEntities1())
    {
        foreach (int aYear in response)
        {
            // access a DbSet from your DbContext here! 
            // To actually fetch data from the database.....
            List<string> paths = db.ClientStatement_Inventory
                                   .Where(x => x.statementYear == aYear)
                                   .Select(y => y.statementPath).ToList();

            allPaths.AddRange(paths);
        }
    }

    return allPaths; // or do whatever with your total results
}
更新:

根据您更新的问题-您真正需要的是:

List<string> paths = new List<string>();

using (var db = new FMBDBPRDEntities1())
{
    List<ClientStatement_Inventory> years = new List<ClientStatement_Inventory>();

    years = years.Where(x => x.statementYear == response[i]).ToList();

    foreach (var year in years)
    {
        paths.Add(year.statementPath);
    }
}
更新3:只是想知道-似乎你从来没有真正访问过DbContext。。。。。我只是在这里猜测——但大致是这样的:

-- select your customers matching a certain criteria
var customers = NorthwindDbContext.Customers.Where(x => x.statementYear == response[i]).ToList();

-- iterate over your customers
foreach (Customer c in customers)
{
     // do whatever with your "Customer" here
}
if (response != null)
{
    List<string> allPaths = new List<string>();

    using (var db = new FMBDBPRDEntities1())
    {
        foreach (int aYear in response)
        {
            // access a DbSet from your DbContext here! 
            // To actually fetch data from the database.....
            List<string> paths = db.ClientStatement_Inventory
                                   .Where(x => x.statementYear == aYear)
                                   .Select(y => y.statementPath).ToList();

            allPaths.AddRange(paths);
        }
    }

    return allPaths; // or do whatever with your total results
}

我用更多的代码更新了我的问题。就我所尝试的而言,foreach语句从未完全贯穿其中。它给了我一个错误:数据不可用。有关可用的intellitrace数据,请参见“本地人”窗口@New_Coder:update my response,向您展示您真正需要的……感谢您的更新。。但是我仍然需要一个for循环来迭代响应,这样我才能得到正确的响应索引?@New_Coder:从你发布的代码中不清楚响应到底是什么-但是如果它是一个集合,你需要迭代它-那么是的,你需要一个循环-但是没有SqlDataReader或任何其他,真的…你太棒了!!!!非常感谢你的帮助,现在我要弄清楚如何在我的控制器中下载这些文件。谢谢你的帮助!