2

我是 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 的东西,您可以在其中加入另一个表中的值。

非常感谢您的帮助

4

2 回答 2

5

You're extremely close! Using your code (and making all properties on House and Person public), here is a method using the LINQ Join method:

var st = GetAllHouses().Join(GetAllPersons(),
    outerKey => outerKey.PersonId,
    innerKey => innerKey.ID,
    (house, person) => new
    {
        house.ID,
        house.Address,
        house.ZipCode,
        PersonFirstName = person.FirstName,
        PersonLastname = person.LastName
    });

Note: I would recommend the GetAllPersons() and the GetAllHouses() methods return IQueryable rather than IEnumerable. Doing so will build the expression (including the join), which means LINQ-to-SQL (or Entities) will build a proper SQL statement with the JOIN included, instead of enumerating the collections and then joining.

Additional information on such can be found here: Returning IEnumerable<T> vs. IQueryable<T>

于 2012-04-10T12:44:01.753 回答
3
using System;
using System.Linq;

class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
}

class Order
{
    public int ID { get; set; }
    public string Product { get; set; }
}

class Program
{
    static void Main()
    {
    // Example customers.
    var customers = new Customer[]
    {
        new Customer{ID = 5, Name = "Sam"},
        new Customer{ID = 6, Name = "Dave"},
        new Customer{ID = 7, Name = "Julia"},
        new Customer{ID = 8, Name = "Sue"}
    };

    // Example orders.
    var orders = new Order[]
    {
        new Order{ID = 5, Product = "Book"},
        new Order{ID = 6, Product = "Game"},
        new Order{ID = 7, Product = "Computer"},
        new Order{ID = 8, Product = "Shirt"}
    };

    // Join on the ID properties.
    var query = from c in customers
            join o in orders on c.ID equals o.ID
            select new { c.Name, o.Product };

    // Display joined groups.
    foreach (var group in query)
    {
        Console.WriteLine("{0} bought {1}", group.Name, group.Product);
    }
    }
}

输出

Sam 买了 Book Dave 买了 Game Julia 买了 Computer Sue 买了 Shirt

于 2012-04-10T12:24:04.373 回答