Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

787
Vistas
.NET 6 IntersectBy and ExceptBy examples

Could someone provide me a small example on how to Use the .NET 6 LINQ IntersectBy and ExceptBy methods? MSDN hasn't got any examples and the one I tried doesn't compile due to CS0411 error. The example I tried:

namespace Test
{
    internal struct Example
    {
        public int X { get; set; }
        public int Y { get; set; }

        public override string ToString()
        {
            return $"{X}, {Y}";
        }
    }

    public class Program
    {
        public static void Main()
        {
            var elements = new List<Example>
            {
                new Example { X = 10, Y = 20 },
                new Example { X = 11, Y = 23 },
            };

            var elements2 = new List<Example>
            {
                new Example { X = 10, Y = 12 },
                new Example { X = 44, Y = 20 },
            };


            //ok
            var union = elements.UnionBy(elements2, x => x.X);
            
            //CS0411 - Why ?
            var intersect = elements.IntersectBy(elements2, x => x.X);

            //CS0411 - Why ?
            var except = elements.ExceptBy(elements2, x => x.X);

            Console.ReadKey();
        }
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Granted the documentation doesn't have any examples, it states that the selector function should select TKey i.e. the type of the second collection. The following should work:

var intersect = elements.IntersectBy(elements2, x => x);
var except = elements.ExceptBy(elements2, x => x);

Although I think this may be closer to what you want:

var intersect = elements.IntersectBy(elements2.Select(e => e.X), x => x.X);

For more complex types, you may want to consider implementing an IEqualityComparer and using the overloads that take one as an argument.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda