【问题标题】:How to use a dynamic LINQ join extension method for multiple conditions如何对多个条件使用动态 LINQ 连接扩展方法
【发布时间】:2015-10-21 08:35:08
【问题描述】:

我正在尝试测试定义 here 的动态连接扩展方法,以在多个条件下连接两个数据集(数据集 1 和数据集 2)。我的加入条件如下图

    outerSelector = "new ( dataset1.ToBeReconciled as col1, dataset1.TransactionDate as col2)";
innerSelector = "new ( dataset2.ToBeReconciled as col1, dataset2.TransactionDate as col2)";

这不会导致任何错误,但同时总是返回零记录,即使我有符合此条件的记录。

知道如何进行这项工作吗?谢谢!

【问题讨论】:

    标签: c# linq join dynamic-linq multiple-conditions


    【解决方案1】:

    也许你应该从一个简单的例子开始,然后尝试完成你所拥有的......我在下面编写了一个简单易懂的例子......

    void Main()
    {
        var fruits = new List<Fruit>
        {
            new Fruit{ GroceryId = 1,Name = "Apple", ProductId = 1},
            new Fruit{ GroceryId = 1,Name = "Orange", ProductId = 2},
        };
    
        var groceries = new List<Grocery>
        {
            new Grocery { GroceryId = 1, Name = "Fruits and Vegetables" },
            new Grocery { GroceryId = 2, Name = "Drinks and snacks" },
        };
    
        var joinedResults = fruits.Join(groceries, // References the groceries declared above,
                                        fruit => fruit.GroceryId,  // Join by GroceryId located on the Fruit
                                        grocery => grocery.GroceryId,  // Join by the GroceryID located on Grocery 
                                        (product, grocery) => new 
                                        {
                                            ProductId = product.ProductId, 
                                            ProductName = product.Name, 
                                            GroceryName = grocery.Name
                                        });
    }
    public class Fruit
    {
        public int ProductId { get; set; }
        public int GroceryId { get; set; }
        public string Name { get; set; }
    }
    
    public class Grocery
    {
        public int GroceryId { get; set; }
        public string Name { get; set; }
    }
    

    【讨论】:

    • 简单的工作正常,但我的要求是我需要多个条件加入。并且加入多个不起作用。
    • 那么是什么阻止您在前一个语句的末尾添加另一个 Join 语句?
    • 什么都没有。它编译完美。但它不起作用。即使存在与连接条件匹配的记录,它也始终显示“枚举未产生任何结果”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 2021-06-22
    • 2014-03-04
    • 1970-01-01
    相关资源
    最近更新 更多