How to use a dynamic LINQ join extension method for multiple conditions

c# dynamic-linq join linq multiple-conditions

Question

I'm trying to test out a dynamic join extension method as defined here to join two datasets(dataset1 and dataset2) on multiple conditions. My join condition is as shown below

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

This does not result in any error, but at the same time always returns zero records, even though I have records that match this condition.

Any idea how to make this work? Thanks!

1
0
7/30/2015 5:54:30 AM

Popular Answer

Perhaps you should start with a simple example, and then try to work through what you have there... I've composed a simple to understand one below..

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

Result

0
7/30/2015 6:48:53 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow