2

I have this requirement,

A product can have multiple images and only one default image. You can determine the default image of the product if the property isDefault is equal to true.

I want to do it in LINQ and lambda but I'm stucked in my code:

private class ProdcutImages
{
    public Int32 ID { get; set; }
    public String ProductID { get; set; }
    public Boolean IsDefault { get; set; }
    public Image Image { get; set; }
    public String FileName { get; set; }
}

public void SetDefaultImage(int productID)
{
    SqlConnection conn = getConnection();
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM product_images WHERE product_id = @ID", conn);
    da.SelectCommand.Parameters.Add(new SqlParameter("@ID", productID));


    DataTable dt = new DataTable();
    da.Fill(dt);

    var imageList = (from tr in dt.AsEnumerable()
                        select new ProdcutImages()
                        {
                            ID = tr.Field<int>("id"),
                            ProductID = tr.Field<string>("productId"),
                            IsDefault = tr.Field<bool>("isDefault"),
                            Image = tr.Field<Image>("image"),
                            FileName = tr.Field<string>("fileName")
                        }).ToList();

    pictureBox1.Image = // ???
    conn.Close();
}
yonan2236
  • 13,371
  • 33
  • 95
  • 141
  • You could make the SQL check for the isDefault-flag, to eliminate the rest of the images. Or is there a need to grab all images at the same time? – NoLifeKing Feb 07 '11 at 15:36

4 Answers4

2

probably this:

var img = imageList.FirstOrDefault(i => i.IsDefault);
if (img != null) { pictureBox1.Image = img.Image; }

or, to take into account the case someone forgot to set the IsDefault field on any image:

var img = imageList.FirstOrDefault(i => i.IsDefault) ?? imageList.FirstOrDefault();
if (img != null) { pictureBox1.Image = img.Image; }

(this would use the first available image if none is set as default)

Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • Your second snippet does meet the OP's requirements because in your example if there isn't a default image available, the first image in the list will be used instead. – Andreas Grech Feb 07 '11 at 15:46
2

If the method is SetDefaultImage, it would make sense to

SELECT * 
FROM product_images 
WHERE product_id = @ID and is_default = 1

or something similar. No point in bringing extra stuff from the database if you don't need to

shA.t
  • 16,580
  • 5
  • 54
  • 111
Ariel Arjona
  • 172
  • 1
  • 10
1
pictureBox1.Image = imageList.FirstOrDefault(pi => pi.IsDefault);

You do not even need to use the ToList for that.

Jaroslav Jandek
  • 9,463
  • 1
  • 28
  • 30
0

Do you just need to select the image out of a set for a particular product where isDefault=true?

I do things a little differently. I define a connection string globally (db) so I'm not certain if this works exactly as is for you, but you should be able to modify the first line to your methods.

Here's what I think you're asking for:

var imageList = db.DataTable
    .Where(w => w.ID == productID && w.IsDefault == true).FirstOrDefault();
pictureBox.Image = imageList.Image;

If there is any chance there are more than one images where IsDefault is true, I think you can look at using SingleOrDefault and trap the error.

nycdan
  • 2,819
  • 2
  • 21
  • 33