如何在页面加载时显示一个产品图片

时间:2014-03-17 09:32:30

标签: asp.net c#-4.0

我有一个表存储产品图片,每个产品图片表中可以有多张图片,指定哪个图片属于哪个产品,我将产品的ProdId存储在图片表中,此时工作正常,但是当我想在产品页面的lage load事件上显示1个特定的pic(最后添加的一个)时,它显示了prod的所有图片,它就像所有那些pics属于另一个prod但是那些实际上属于一个产品,请帮助我只是想在页面加载时只显示一张图片(最后添加一张)而不是所有图片?

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillProds();
            }
        }
 protected void FillProds()
        {
            string _session = Session["lang"].ToString();
            if (_session == "tr-TR")
            {
                var _tblProd = (from x in _db.Tbl_Prods
                                join y in _db.Tbl_Imgs
                                on x.ProdId equals y.ProdId
                                join z in _db.Tbl_Cats
                                on x.CatId equals z.CatId
                                orderby x.DbDate descending
                                select new
                                {
                                    ProdId = x.ProdId,
                                    ProdCode = x.ProdCode,
                                    ProdName = x.ProdNameTr,
                                    ImgUrl = y.ImgUrl,
                                }).ToList();

                lvListAll.DataSource = _tblProd;
                lvListAll.DataBind();
            }
            else if (_session == "ru-RU")
            {
                var _tblProd = (from x in _db.Tbl_Prods
                                join y in _db.Tbl_Imgs
                                on x.ProdId equals y.ProdId
                                join z in _db.Tbl_Cats
                                on x.CatId equals z.CatId
                                orderby x.DbDate descending
                                select new
                                {
                                    ProdId = x.ProdId,
                                    ProdCode = x.ProdCode,
                                    ProdName = x.ProdNameRu,
                                    ImgUrl = y.ImgUrl,
                                }).ToList();

                lvListAll.DataSource = _tblProd;
                lvListAll.DataBind();
            }
        }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

var images = (from p in _db.Tbl_Prods
              join img in _db.Tbl_Imgs on p.ProdId equals img.ProdId 
              group img by p.ProdId into g
              select new { g.Key, MaxImageId = g.Max(x => x.ImageId) })
              .Select(x => x.MaxImageId).ToArray();

 var prods = (from p in  _db.Tbl_Prods
              join img in _db.Tbl_Imgs on p.ProdId equals img.ProdId 
              where images.Contains(img.ImageId)
              select new 
              { 
                     ProdId = x.ProdId,
                     ProdCode = x.ProdCode,
                     ProdName = x.ProdNameRu,
                     ImgUrl = img.ImgUrl
              });