0

I have two tables, Parts and Blob.

db.Parts

Id   name   x    y   z  partimageid
-----------------------------------
1    bolt   30   40  5       4 
2    screw  33   4   6     null

db.Blob

Id   content      name
------------------------------
4    fsbfvb       picture.png

How can I get a part detail with image content?

Currently I am doing like this:

public async Task<IHttpActionResult> GetPart(int id)
{    
    var result = from part in db.Parts
                 join image in db.Blob on part.PartImageId equals image.Id
                 where part.Id == id
                 select new { part, image.Content};
    return Ok(result);
}

It's working, but if image is not there - part also coming as null. what am I doing wrong here?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ebk
  • 305
  • 8
  • 20

1 Answers1

0

Your LINQ statement is executed as an INNER JOIN. If you want a LEFT JOIN then you could modify it to something like this:

var result = from part in db.Parts
                 join image in db.Blob on part.PartImageId equals image.Id into images
                 from image in images.DefaultIfEmpty()
                 where part.Id == id
                 select new { part, image.Content};