LINQ to SQL join when there aren't results

Posted by Boarder2 on Stack Overflow See other posts from Stack Overflow or by Boarder2
Published on 2010-04-02T02:00:28Z Indexed on 2010/04/02 2:03 UTC
Read the original article Hit count: 441

Filed under:
|
|
|
|

Given the following database structure alt text

I'm trying to write a LINQ query that will return images grouped by tags it's associated with. So far I've got this:

var images = from img in db.Images
    join imgTags in db.ImageTags on img.idImage equals imgTags.idImage
    join t in db.Tags on imgTags.idTag equals t.idTag
    where img.OCRData.Contains(searchText.Text)
    group img by new { t.TagName } into aGroup
    select new
    {
        GroupName = aGroup.Key.TagName,
        Items = from x in aGroup
        select new ImageFragment()
        {
             ImageID = x.idImage,
             ScanDate = x.ScanTime
        }
    };

Which works great. However, I also want to return Images that do not have any tags associated with them in a group of "(Untagged)" or something. I can't wrap my head around how I would do this without inserting a default tag for every image and that seems like generally not a very good solution.

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about join