我是 LINQ 的新手,如果有人问我的问题,我很抱歉
我有 2 节课
public class Person
{
int ID {get;set;}
string FirstName {get;set;}
string LastName {get;set;}
}
和
public class House
{
int ID {get;set;}
string Address {get;set;}
string ZipCode {get;set;}
int PersonId {get;set;}
}
我将房屋列表保存在 IEnumerable 列表中
IEnumerable<House> ListHouses = GetAllHouses();
GetAllHouses 从数据库中返回房屋列表
我想在 LINQ 中使用 Lamda select 来执行以下操作
var st = ListHouses .Select(h => new
{
id = h.ID,
Address= h.Address,
Zip= h.ZipCode ,
PersonFirstName = GetPersonByID(h.PersonId ).FirstName,
PersonLastname = GetPersonByID(h.PersonId ).lastname
});
其中 GetPersonByID 返回Person
具有给定 ID 的 Type 对象。然后我取他的名字和姓氏。
我的问题是这样的:
而不是为变量(personFirstName 和 PersonLastName)获取 Person 2 次有没有一种方法可以让我一次获取它然后使用它。就像是
PersonForId = GetPersonByID(h.PersonId)
PersonFirstName = PersonLastName.FirstName,
PersonLastname = PersonLastName.lastname
我正在寻找类似于 Join in SQL 的东西,您可以在其中加入另一个表中的值。
非常感谢您的帮助