Gilthayllor Sousa
Gilthayllor Sousa

Reputation: 33

Visual Studio 2019 Intellisense for Linq Joined To Class Not Appearing

My VS intellisense does not work when i do lambda queries, like Join, GroupJoin, etc. The properties of the second model never appear in the suggestions. I'm sorry for my english :)

See the images:

First Key

Second Key

Upvotes: 3

Views: 412

Answers (2)

Eddy Aper
Eddy Aper

Reputation: 1

Apparently there is a workaround, by placing the second key selector and the result selector between brackets ().

    class Person
    {
        public string Name { get; set; }
    }
    class Pet
    {
        public string Name { get; set; }
        public Person Owner { get; set; }
    }
    public static void JoinEx1()
    {
        Person magnus = new Person { Name = "Hedlund, Magnus" };
        Person terry = new Person { Name = "Adams, Terry" };
        Person charlotte = new Person { Name = "Weiss, Charlotte" };

        Pet barley = new Pet { Name = "Barley", Owner = terry };
        Pet boots = new Pet { Name = "Boots", Owner = terry };
        Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
        Pet daisy = new Pet { Name = "Daisy", Owner = magnus };

        List<Person> people = new List<Person> { magnus, terry, charlotte };
        List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy };

        var query =
            people.Join(pets,
                        person => person,
                        (pet => pet.Owner), // intellisense here
                        ((person, pet) =>   // intellisense here
                            new { OwnerName = person.Name, Pet = pet.Name }));

Afterwards the brackets can be removed, but intellisense helps a lot on complicated object structures.

Upvotes: 0

Michael Jones
Michael Jones

Reputation: 1910

As @JeroenMostert said, this is a known bug. If you really want the intellisense, you can specify your types; with result2 you'll get intellisense.

You just have to decide if having to explicitly set your types is worth it, especially as it means you can't really return an anonymous object.

Personally, I don't think making your code more verbose is worth it as not having intellisense won't prevent you from setting up your lambda.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var people = new List<Person>();
            var employees = new List<Employee>();

            var result = employees.Join(people, x => x.Id, y => y.Id, (x, y) => new JoinedItem{ Id = x.Id, Name = y.Name });

            var result2 = employees.Join<Employee, Person, int, JoinedItem>(people, x => x.Id, y => y.Id, (x, y) => new JoinedItem { Id = x.Id, Name = y.Name });

        }
    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class JoinedItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Upvotes: 3

Related Questions