SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)

    SQL to LINQ help.

    Two tables, Categories and Forum. One to many relationship. Building a dropdown from LINQ. Classes work ok but my syntax for the join, or something, is off.

    Here it is, in it's current state, after many edits:

    Code VBNET:
    DropDownList1.DataSource = From f In wrdc.Forums Join c In wrdc.Categories On f.CategoryID Equals c.ID Select f, c Where f.IsVisible = True And c.IsVisible = True

    Doing a basic select works fine:

    Code VBNET:
    DropDownList1.DataSource = From f In wrdc.Forums Select f Where f.IsVisible = True Order By f.Sequence

    Both Categories and Forums have a sequence field for ordering. I need to get forum info sorted correctly by cateogry.sequence and then forum.sequence. Thanks in advance for any LINQ help.

  2. #2
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Is the association defined in the LINQ to SQL class?

    If so you don't need the join it is kinda/sorta automatic.

    For example say you have a article table with a foreign key association to a category table:
    Code C#:
    var articles = from p in dataContext.articles
    select new {
    articleID = p.ID,
    categoryName = p.category.categoryName,
    title = p.title,
    created = String.Format("{0:d} at {0:t}", p.created),
    createdBy = p.createdBy
    }

    You can use the p.category.categoryName without the join. Of course join is still needed in some cases. Also, the select goes at the end.

    Of course I'm talking form the C# end so take that into consideration.

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Ok, I looked into the ordering of the linq a little more and this got me what I wanted.


    DropDownList1.DataSource = _
    From f In wrdc.Forums _
    Join c In wrdc.Categories On c.ID Equals f.CategoryID _
    Where c.IsVisible = True AndAlso f.IsVisible = True _
    OrderBy c.Sequence Ascending, f.Sequence Ascending _
    Select f


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •