gpt4 book ai didi

c# - 如何使用 LINQ 执行动态连接

转载 作者:行者123 更新时间:2023-11-30 22:02:40 25 4
gpt4 key购买 nike

我有两个收藏。每个集合都包含特定类型的实例。我需要使用每个集合中实例的属性之一来加入这两个集合。问题是我只会在运行时才知道要使用哪个属性进行连接。那么如何为动态 Join 编写 LINQ 查询呢?这是带有静态连接的 LINQ 查询的代码。在以下示例代码中,您会注意到我使用 MyTran.MyAmountUSD = YourTran.YourAmountUSD 加入两个集合。但在实际场景中,我只会在运行时才知道使用哪个属性进行连接。所以有时我可能需要加入 MyTran.MyAmountGBP = YourTran.YourAmountGBP。

class MyTran
{
public int Id { get; set; }
public double MyAmountUSD { get; set; }
public double MyAmountGBP { get; set; }
}

class YourTran
{
public int Id { get; set; }
public double YourAmountUSD { get; set; }
public double YourAmountGBP { get; set; }
}

class Program
{
static void Main(string[] args)
{
List<MyTran> fMyTranList = new List<MyTran>();
List<YourTran> fYourTranList = new List<YourTran>();

fMyTranList.Add(new MyTran { Id = 1, MyAmountGBP = 100, MyAmountUSD = 1000 });
fMyTranList.Add(new MyTran { Id = 2, MyAmountGBP = 101, MyAmountUSD = 2000 });

fYourTranList.Add(new YourTran { Id = 11, YourAmountGBP=100, YourAmountUSD=1000 });
fYourTranList.Add(new YourTran { Id = 12, YourAmountGBP = 102, YourAmountUSD = 3000 });

var query = from fMyTrans in fMyTranList
join fYourTrans in fYourTranList
on fMyTrans.MyAmountUSD equals fYourTrans.YourAmountUSD
select new
{
MyId = fMyTrans.Id,
YourId = fYourTrans.Id,
MyAmtUSD = fMyTrans.MyAmountUSD,
MyAmtGBP = fMyTrans.MyAmountGBP,
YourAmtUSD = fYourTrans.YourAmountUSD,
YourAmtGBP = fYourTrans.YourAmountGBP
};

foreach (var fMatch in query)
{
Console.WriteLine(fMatch);
}
}
}

最佳答案

解决此问题的第一个线索是使用 lamda syntax 重写您的连接对于 Join

var query = fMyTranList.Join(fYourTranList,
a => a.MyAmountGBP,
b => b.YourAmountGBP,
(c,d) => new
{
MyId = c.Id,
YourId = d.Id,
MyAmtUSD = c.MyAmountUSD,
MyAmtGBP = c.MyAmountGBP,
YourAmtUSD = d.YourAmountUSD,
YourAmtGBP = d.YourAmountGBP
});

直播:http://rextester.com/OGC85986

这应该清楚地表明,使这种动态化需要传递给您的“通用”连接函数 3 个函数

  1. 一个Func<MyTran,TKey> LHS选择键的功能
  2. 一个Func<YourTran,TKey> RHS选择键的功能
  3. 一个Func<MyTran,YourTran,TResult>选择结果的功能

所以从那里你可以使用反射来使这一切变得有点动态:

public static class DynamicJoinExtensions
{
public static IEnumerable<dynamic> DynamicJoin(this IEnumerable<MyTran> myTran, IEnumerable<YourTran> yourTran, params Tuple<string, string>[] keys)
{
var outerKeySelector = CreateFunc<MyTran>(keys.Select(k => k.Item1).ToArray());
var innerKeySelector = CreateFunc<YourTran>(keys.Select(k => k.Item2).ToArray());

return myTran.Join(yourTran, outerKeySelector, innerKeySelector, (c, d) => new
{
MyId = c.Id,
YourId = d.Id,
MyAmtUSD = c.MyAmountUSD,
MyAmtGBP = c.MyAmountGBP,
YourAmtUSD = d.YourAmountUSD,
YourAmtGBP = d.YourAmountGBP
}, new ObjectArrayComparer());
}

private static Func<TObject, object[]> CreateFunc<TObject>(string[] keys)
{
var type = typeof(TObject);
return delegate(TObject o)
{
var data = new object[keys.Length];
for(var i = 0;i<keys.Length;i++)
{
var key = type.GetProperty(keys[i]);
if(key == null)
throw new InvalidOperationException("Invalid key: " + keys[i]);
data[i] = key.GetValue(o);
}
return data;
};
}

private class ObjectArrayComparer : IEqualityComparer<object[]>
{

public bool Equals(object[] x, object[] y)
{
return x.Length == y.Length
&& Enumerable.SequenceEqual(x, y);
}

public int GetHashCode(object[] o)
{
var result = o.Aggregate((a, b) => a.GetHashCode() ^ b.GetHashCode());
return result.GetHashCode();
}
}
}

匹配您的示例的用法是:

var query = fMyTranList.DynamicJoin(fYourTranList, 
Tuple.Create("MyAmountGBP", "YourAmountGBP"));

但是因为键是params你可以传递任意多个:

var query = fMyTranList.DynamicJoin(fYourTranList, 
Tuple.Create("MyAmountGBP", "YourAmountGBP"),
Tuple.Create("AnotherMyTranProperty", "AnotherYourTranProperty"));

实例:http://rextester.com/AAB2452

关于c# - 如何使用 LINQ 执行动态连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26656604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com