1

I wonder how to convert SqlDataReader to LINQ way?

Here is my SqlDataReader code.

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];
}

This code works fine.

I've tried to code in LINQ but it doesn't work.

Here's the LINQ code that I've tried, but I have no idea how to extract all records of a.identity_num, and put them into an array like the code above(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();

[UPDATE Question]

This is what I am trying now.

I can't extract the right records from my table.

But the page display: 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];
         }
.............
............
...........
3
  • What do you need that annonymus type for? If you don't need it, change the new { c.period } to c.period Commented May 20, 2013 at 10:53
  • @CSharpie But the page display: System.String[] Commented May 20, 2013 at 11:15
  • There is no but, System.String[] is not an annonymus type, or ist it? Commented May 20, 2013 at 11:16

3 Answers 3

3

Updated to match changed question:

Don't select a new anonymous object, just select the value:

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

Fully updated change should be

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];

From the comments:

  • The error message generated by having ToString() in the code indicates to us that period is a string anyhow and so the call can be removed.

  • To get count, int Count = arr.Length

  • The for-loop has been removed because it is redundant, using the ToArray() function achieves the same result.

Sign up to request clarification or add additional context in comments.

8 Comments

I've updated my question. But the page display: <>f__AnonymousType15`1[System.String][]
@user2401077 Check the updated answer. Also if you are only going to use the first two values you might want to use var arr = Query.Take(2).ToArray()
Why do you delete the for loop?
And also I still need int Count = Query.Count();
Then do int Count = arr.Length; Your for-loop became obsolete. All you need is the variable 'arr'. Its an array of strings. More precise arr is System.String[].
|
0

you should need to convert linkq to 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];

2 Comments

I've updated my question. But the page display: <>f__AnonymousType15`1[System.String][]
Why do you delete the for loop? And also I still need int Count = Query.Count();
0

You need to convert linq query to array. Bellow code should do this:

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

3 Comments

That creates an anonymous type rather that selecting the value like the reader code does. Also the reader code is grabbing period not identity_num
I've updated my question. But the page display: <>f__AnonymousType15`1[System.String][]
Those = need to be ==, also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.