繁体   English   中英

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

[英]Visual Studio 2019 Intellisense for Linq Joined To Class Not Appearing

当我执行 lambda 查询(例如 Join、GroupJoin 等)时,我的 VS 智能感知不起作用。第二个模型的属性从未出现在建议中。 我为我的英语感到抱歉:)

看图片:

第一把钥匙

第二把钥匙

正如@JeroenMostert 所说,这是一个已知的错误。 如果你真的想要智能感知,你可以指定你的类型; 使用 result2,您将获得智能感知。

您只需要决定是否值得显式设置您的类型,尤其是因为这意味着您无法真正返回匿名对象。

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

显然有一种解决方法,将第二个键选择器和结果选择器放在括号 () 之间。

    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 }));

之后可以移除括号,但智能感知对复杂的对象结构有很大帮助。

暂无
暂无

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

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