Reputation: 13
I have a Database data I have written a join query to join two tables and then select the data based on PhotoContestID, but I want unique values Below is the join query and its result
select *
from PhotoContest.ContestImageLibrary
Left Join
PhotoContest.ContestCategories On
PhotoContest.ContestImageLibrary.PhotoContestID = PhotoContest.ContestCategories.PhotoContestID;
I want to get unique values based on ImageID FirstorDefault. I don't want duplicated records, like for eg, ImageID
I have written the below code to get the details but I am getting duplicates. I have tried using Distinct
at the end but no use.
var voteList = (from i in context.ImageLibrary
join p in context.ContestCategories on i.PhotoContestID equals p.PhotoContestID
select new VoteLists()
{
PhotoContestID = i.PhotoContestID,
ImageID = i.ImageID,
ImageCaption = i.ImageCaption,
ImageName = i.ImageName,
categoryName = p.categoryName,
isImageAccepted = i.isImageAccepted,
catID = p.Id,
empID = i.empID,
votingType = p.votingType
}).Where(x => x.PhotoContestID == id).ToList();
}
What shall I write exactly in my backend to get the desired unique result or maybe I can't get unique values based on ImageID.
Thanks
Upvotes: 0
Views: 142
Reputation: 305
The problem is that multiple categories match each single image.
I would say you need to use a GroupBy with all duplicated fields in the key, and that will force you to specify how to manage the multiple matching categories in the value part.
Upvotes: 1