none
How to read one row record data in multiple column using LINQ VB RRS feed

  • Question

  • I am building website using VS Web Developer 2008 with VB code. The website has database file MyDatabase.mdf that has one table MyTable. This table has 101 column as follow:  MyTblCol0, MyTblCol1, MyTblCol2, MyTblCol3, MyTblCol4, …….etc until……. MyTblCol100.

    I want to read my first record data MyTable for all 101 columns (MyTblCol0, MyTblCol1, MyTblCol2, MyTblCol3, MyTblCol4, …….etc until……. MyTblCol100) and store the record data into array MyArray(100).

    I appreciate if someone could write LINQ VB code to handle this problem.

    Thanks,

    Fialbz

    Thursday, September 30, 2010 2:37 PM

All replies

  • Sorry I didn't see that the question was last year and not last week...
    Friday, November 11, 2011 2:26 PM
  • @Alain

    That is nothing compared to my answer to a post and later I discovered it was from 2006 LOL

    @fialbz

    I don't know if you are asking of how to skip writing 100 field in linq query? if that is your question here is the answer :

    Dim db As New NorthwindDataContext("connection_string")
    Dim q  = db.GetTable(Of customer).Take(1)

    Let see if an answer can be helpful after 1 year :)

     


    Wednesday, January 4, 2012 5:33 PM
  • I advise you to use method  FirstOrDefault()
    thank

    So B
    Thursday, January 5, 2012 11:11 AM
  • I advise you to use method  FirstOrDefault()
    thank

    So B

    Hi, that will work but :)

    Let say you have 100,000 record in a table. using FirstOrDefault will fetch all the 100,000 row record from the sql server, then it will get the first row (which is correct)

    Doing it by .Take(1) will fetch only Top(1) (1 Row from the server)

    It has performance hit.

     


    Thursday, January 5, 2012 12:45 PM
  • thank Sam  for your precision !

     

     


    So B
    Thursday, January 5, 2012 12:52 PM
  • Why ? can yu give us some details?
    Friday, January 6, 2012 10:14 PM
  • I don't think people have read or understood the OP's question.  My read is that he wants a way to write a query that, from the first row of a specified table with 101 columns (fields), reads the first 100 columns of data (again, from the first row) into an array of dimension 100.  Am I the only one who interprets the OP's question this way?  Unfortunately, I don't have an answer, but I thought I would at least try to illuminate the real question.
    Monday, January 9, 2012 5:23 AM
  • Hi TTRoadHog

    You was right with your reading to the question, I did not noticed that array request in the OP question.

    Actually I am interested in such kind of question so that I was working on finding a solution to the problem, but all my trial and search failed, until I ask one of my expert friend Cetin Basoz about it and his first answer was this can be done by reflection

    Here is his response which I like to share

    Dim data = (from c in db.customers select c).Take(1).FirstOrDefault()
    
    Dim arr_result = Convert_LinqResult_To_1DArray(data)
    For i = 0 to arr_result.Length -1     Debug.Print(arr_result(i)) Next
    Function Convert_LinqResult_To_1DArray(ByVal linq_result As ObjectAs Array     dim fields() = linq_result.GetType().GetProperties().ToArray()     dim dataArr(fields.count()) as Object     for i = 0 to fields.Count ()-1     dataArr(i)  = fields(i).GetValue(linq_result,Nothing)     next     Return dataArr End Function



    Thursday, January 12, 2012 9:22 AM