Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 434427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:16:24+00:00 2026-05-12T20:16:24+00:00

C#: I have a collection of objects . T has 2 properties. Property A

  • 0

C#: I have a collection of objects . T has 2 properties. Property A and Property B. The rule that this collection needs to adhere to is that the combination of values for A and B must be unique within the collection. In other words, A and B need to serve as a composite primary key.

Is there an operation in Linq I can use to check this condition? I’d expect it to be something like

if (items.Select(x => x.Name).Distinct().Count() != items.Select(x => x.Name).Count())

The above statement is how I would check whether there are items in the collection which have duplicate Names, but I don’t know how to do it for more than one property.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:16:25+00:00Added an answer on May 12, 2026 at 8:16 pm

    Use an anonymous type to select the composite key, e.g.

    int totalCount = items.Count();
    int distinctCount = items.Select(x => new { x.Name, x.Other })
                             .Distinct()
                             .Count();
    

    Anonymous types automatically implement equality and hashcodes based on their properties (and the default equality comparer for the types of those properties).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection FileTypes that holds objects of type FileType. That type has
In this hypothetical example, imagine I have an object FooSet that has five properties
I have a collection that is IEnumerable<Transaction> . Transaction has several properties such as
I have a model that has a property which is a collection. I can
I have an object that has a child collection: class Person { public string
I have a collection companies. Every object in this collection has lastcall-param. It's a
Assume I have a class called MyClass that has two properties (int Id and
I have a collection of Objects with NoteProperties. One of these properties is an
I have a Report object that has Recipients property (of String datatype). The Recipients
So I have a collection of objects that may be of multiple type. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.