Visual Studio 2019 Intellisense for Linq 加入 Class 未出现
Visual Studio 2019 Intellisense for Linq Joined To Class Not Appearing
当我执行 lambda 查询(如 Join、GroupJoin 等)时,我的 VS 智能感知不起作用。第二个模型的属性从未出现在建议中。对不起我的英语:)
查看图片:
正如@JeroenMostert 所说,这是一个已知错误。如果你真的想要智能感知,你可以指定你的类型;使用 result2,您将获得智能感知。
您只需要决定是否必须显式设置您的类型是值得的,尤其是因为这意味着您不能真正 return 一个匿名对象。
就我个人而言,我不认为让代码更冗长是值得的,因为没有智能感知不会阻止您设置 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 }));
之后括号可以去掉,但是intellisense对复杂的对象结构有很大帮助。
当我执行 lambda 查询(如 Join、GroupJoin 等)时,我的 VS 智能感知不起作用。第二个模型的属性从未出现在建议中。对不起我的英语:)
查看图片:
正如@JeroenMostert 所说,这是一个已知错误。如果你真的想要智能感知,你可以指定你的类型;使用 result2,您将获得智能感知。
您只需要决定是否必须显式设置您的类型是值得的,尤其是因为这意味着您不能真正 return 一个匿名对象。
就我个人而言,我不认为让代码更冗长是值得的,因为没有智能感知不会阻止您设置 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 }));
之后括号可以去掉,但是intellisense对复杂的对象结构有很大帮助。