简体   繁体   中英

How to remove or get distinct values in C# LINQ query?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM