简体   繁体   English

Visual Studio 2019 Intellisense for Linq 加入课程未出现

[英]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.当我执行 lambda 查询(例如 Join、GroupJoin 等)时,我的 VS 智能感知不起作用。第二个模型的属性从未出现在建议中。 I'm sorry for my english :)我为我的英语感到抱歉:)

See the images:看图片:

第一把钥匙

第二把钥匙

As @JeroenMostert said, this is a known bug.正如@JeroenMostert 所说,这是一个已知的错误。 If you really want the intellisense, you can specify your types;如果你真的想要智能感知,你可以指定你的类型; with result2 you'll get intellisense.使用 result2,您将获得智能感知。

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.就我个人而言,我不认为让你的代码更冗长是值得的,因为没有智能感知不会阻止你设置你的 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; }
    }
}

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.之后可以移除括号,但智能感知对复杂的对象结构有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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