1

我想知道如何将 SqlDataReader 转换为 LINQ 方式?

这是我的 SqlDataReader 代码。

string strConn = "server=xxx.com;database=mydb;User ID=test;Password=test;Trusted_Connection=true;";
SqlConnection myConn = new SqlConnection(strConn);
myConn.Open();

string strSQL = "select period from timetable_view where identity_num = '" + Identity_NUM + "' week = '3'";
SqlCommand myCommand = new SqlCommand(strSQL, myConn);
SqlDataReader myDataReader = myCommand.ExecuteReader();

int Count = Add_CourseConfirmedQuery.Count();

Random rnum = new Random();
string[] arr = new string[Count];
for (int i = 0; i < Count; i++)
{
    myDataReader.Read();
    arr[i] = myDataReader["period"].ToString();
}
ViewBag.test = arr[0];
ViewBag.test2 = arr[1];

myCommand.Cancel();
myDataReader.Close();
myConn.Close();
myConn.Dispose();

string[] input2 = new string[Count];

for (int i = 0; i < Count; i++)
{
    input2[i] = arr[i];
}

这段代码工作正常。

我尝试在 LINQ 中编写代码,但它不起作用。

这是我尝试过的 LINQ 代码,但我不知道如何提取 的所有记录a.identity_num,并将它们放入一个数组中,如上面的代码(SqlDataReader)。

var test = from a in timetable_view
           where a.identity_num = Identity_NUM && a.week = "3"
           select a new{a.identity_num};

ViewBag.test = test.FirstOrDefault();

[更新问题]

这就是我现在正在尝试的。

我无法从我的表中提取正确的记录。

但是页面显示:System.String[]

var Query = from m in db.members
            join d in db.departments on m.department equals d.department_id
            join s in db.select_list on m.member_id equals s.member_id
            join c in db.courses on s.kkk_id equals c.kkk_id
            join t in db.teachers on c.teacher equals t.teacher_id
            where m.identity_num == Identity_NUM && c.week == "3"
            select c.period;
            var PA = Query.ToArray();
            int Count = Query.Count();


string[] arr = new string[Count];
            for (int i = 0; i < Count; i++)
            {
                arr[i] = PA.ToString();
            }
            ViewBag.test = arr[0];
            ViewBag.test2 = arr[1];
.............
............
...........

string[] input2 = new string[Count];
         for (int i = 0; i < Count; i++)
         {
            input2[i] = arr[i];
         }
.............
............
...........
4

3 回答 3

3

更新以匹配更改的问题:

不要选择新的匿名对象,只需选择值:

var arr= (from a in timetable_view
           where a.identity_num = Identity_NUM && a.week = "3"
           select a.period).ToArray();

完全更新的变化应该是

var Query = from m in db.members
            join d in db.departments on m.department equals d.department_id
            join s in db.select_list on m.member_id equals s.member_id
            join c in db.courses on s.kkk_id equals c.kkk_id
            join t in db.teachers on c.teacher equals t.teacher_id
            where m.identity_num == Identity_NUM && c.week == "3"
            select c.period;


var arr = Query.ToArray();    

ViewBag.test = arr[0];
ViewBag.test2 = arr[1];

从评论:

  • 代码中包含 ToString() 生成的错误消息向我们表明,句点无论如何都是一个字符串,因此可以删除调用。

  • 要计数,int Count = arr.Length

  • for 循环已被删除,因为它是多余的,使用 ToArray() 函数可以达到相同的结果。

于 2013-05-20T09:45:27.507 回答
0

您需要将 linq 查询转换为数组。波纹管代码应该这样做:

var test = (from a in school_timetable_view
           where a.identity_num == Identity_NUM && a.week == "3"
           select a new{a.identity_num}).ToArray();
于 2013-05-20T09:31:08.200 回答
0

您应该需要将 linkq 转换为 toArray:

var TEST = (from a in timetable_view
           where a.identity_num = Identity_NUM && a.week = "3"
           select a new{a.identity_num}).ToArray();

var Query = from m in db.members
            join d in db.departments on m.department equals d.department_id
            join s in db.select_list on m.member_id equals s.member_id
            join c in db.courses on s.kkk_id equals c.kkk_id
            join t in db.teachers on c.teacher equals t.teacher_id
            where m.identity_num == Identity_NUM && c.week == "3"
            select c.period.ToString();


var arr = Query.ToArray();    

ViewBag.test = arr[0];
ViewBag.test2 = arr[1];
于 2013-05-20T09:37:25.270 回答