【问题标题】:LINQ joining values from different classesLINQ 连接来自不同类的值
【发布时间】:2012-04-10 20:53:40
【问题描述】:

我是 LINQ 新手,如果有人问我的问题,我很抱歉

我有两节课

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 返回具有给定 ID 的 Person 类型的对象。然后我取他的名字和姓氏。

我的问题是这样的:

而不是为变量(personFirstName 和 PersonLastName)获取 Person 2 次有没有一种方法可以让我一次获取它然后使用它。类似的东西

PersonForId = GetPersonByID(h.PersonId)
PersonFirstName =  PersonLastName.FirstName,
PersonLastname = PersonLastName.lastname

我正在寻找类似于 Join in SQL 的东西,您可以在其中连接另一个表中的值。

非常感谢您的帮助

【问题讨论】:

标签: c# linq lambda


【解决方案1】:

你非常接近!使用您的代码(并将 House 和 Person 上的所有属性公开),这是一个使用 LINQ Join 方法的方法:

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

注意:我建议 GetAllPersons() 和 GetAllHouses() 方法返回 IQueryable 而不是 IEnumerable。这样做将构建表达式(包括连接),这意味着 LINQ-to-SQL(或实体)将构建一个包含 JOIN 的适当 SQL 语句,而不是枚举集合和 then 连接。

可以在此处找到有关此类的其他信息:Returning IEnumerable<T> vs. IQueryable<T>

【讨论】:

    【解决方案2】:
    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);
        }
        }
    }
    

    输出

    山姆买了书 戴夫买了游戏 朱莉娅买了电脑 苏买了衬衫

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 2022-11-22
      • 2012-07-22
      • 2015-11-04
      相关资源
      最近更新 更多