0
votes

When Iam doing a project i got confused in a place to retrieve only unique value from table. for that i distinct , groupby clauses in the linq query, but I got some error . I have two tables and the id of category table is foreign key and I need to retrieve only distinct id from the another gallery table

model

public partial class Gallery
{
    public int Id { get; set; }
    public int CategoryId { get; set; }
    public string ImageTitle { get; set; }
    public string Image { get; set; }

    public virtual Category Category { get; set; }
}




   public Category()
    {
        this.Galleries = new HashSet<Gallery>();
    }

    public int Id { get; set; }
    public string CategoryName { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Gallery> Galleries { get; set; }
}

this is the model the categoryid is the foreign key to the gallery class

controller

   public ActionResult GalleryCata()
    {
        var res = db.Galleries.ToList();
        return View(res);
    }

view

 @model List<ThaniyamBank.Models.Gallery>

  @for (var j = 0; j < Model.Count(); j++)
        {
            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                <div class="hovereffect">
                    <img src="@Url.Content("~/GalleryImages/" + @Html.DisplayFor(m => m[j].Image))" class="img-responsive" alt="" />
                    <div class="overlay">
                        <h2>@Html.DisplayFor(m => m[j].Category.CategoryName) </h2>
                        <a class=" fancybox info " href="@Url.Content("~/GalleryImages/" + @Html.DisplayFor(m => m[j].Image))" data-fancybox-group="gallery"><i class="fa fa-eye" aria-hidden="true"></i></a>
                        <a class="info " href="gallery.html"><i class="fa fa-link" aria-hidden="true"></i></a>
                    </div>
                </div>
            </div>
        }

I need to display the images based on the category. when i run the application i get all the images from the table and not by based on the category . can anyone please help me to solve the problem ??

1
I don't see any "distinct , groupby". Show what you tried, which will probably clarify what you mean by "unique". And show the error. "I got some error" doesn't mean much. - Gert Arnold
when I give var res = db.Galleries.Select(x=>x.CategoryId).Distinct().ToList(); in controller, got the error The model item passed into the dictionary is of type 'System.Collections.Generic.List1[System.Int32]', but this dictionary requires a model item of type 'System.Collections.Generic.List1[A.Models.Gallery]'. - user256
Pretty clear error message, isn't it? - Gert Arnold
by changing list to ienumerable shows the error Cannot apply indexing with [] to an expression of type 'IEnumerable<Gallery>'. If i just write db without list and ienumerable then how can i get the collection of data from table ?? - user256

1 Answers

0
votes

Select tells EF to retrieve a certain value, so db.Galleries.Select(x=>x.CategoryId) returns a collection of integer, therefore you get the error message.

What you want is to use a Where clause:

var res = db.Galleries.Where(x => x.CategoryId == selectedCategoryId).ToList();

which returns IList<Gallery>. You will need to pass int selectedCategoryId to your action.

Distinct is only needed if you expect that the same Gallery is contained in the same Category multiple times.