none
Reading 1 value from Linq RRS feed

  • Question

  •  

    hi there,

    I've been experimenting with link and I've found a need where I need to just read the first row of many in the results.

    If I was reading all rows then I'd do a FOR .. EACH loop and it would be fine, but I can't find any way to read just 1 column from the first row.... any ideas?

     

    Here's an example:

     

    Dim db As New WEDBDataContext

    Dim WEDBDataSet_AuthorsLinq = From Author In db.Authors _

    Join artauth In db.Art_Auths On Author.A_ID Equals artauth.A_id _

    Where artauth.Id = ArticleID.ToString Order By artauth.Rank _

    Select Author.Author, Author.A_ID

     

    If WEDBDataSet_AuthorsLinq.Count = 1 Then

    Authors = "<a href='/Contents/AuthorDetails.aspx?AID=" & DISPLAY.FIRSTAUTHOR_ID & "' class='Authors'>" & DISPLAY.FIRSTAUTHOR_AUTHOR & "</a>"

    Else

    .....

    end if

     

    Thanks

    Ed

    Friday, November 23, 2007 11:24 AM

Answers

All replies

  • Ed,

     

    You can use any of the LINQ operators in LINQ to DataSet (http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx). In particular, you can use the First or FirstOrDefault operator to get one result. If you want the first few results, you can Take. Let me know if you have any problems using these operators.

     

    Thanks,

    Erick

    Tuesday, November 27, 2007 1:00 AM
  •  

    Excellent, thanks Erick, that did the trick and I've got it sorted using .FirstOrDefault().

     

    I'm still trying to get to grips with Linq so sorry if this was a silly question.

     

    Ed

     

    Tuesday, November 27, 2007 9:28 AM