我有两个收藏。每个集合都包含特定类型的实例。我需要使用每个集合中实例的属性之一来连接这两个集合。问题是我只会在运行时才知道使用哪个属性进行连接。那么如何编写动态连接的 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
解决这个问题的第一个线索是使用lamda 语法重写您的连接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
});
Run Code Online (Sandbox Code Playgroud)
直播: http: //rextester.com/OGC85986
这应该清楚地表明,实现这种动态需要传递给您的“通用”连接函数 3 个函数
Func<MyTran,TKey>
选择 LHS 键的功能Func<YourTran,TKey>
选择 RHS 键的功能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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
与您的示例相匹配的用法将是:
var query = fMyTranList.DynamicJoin(fYourTranList,
Tuple.Create("MyAmountGBP", "YourAmountGBP"));
Run Code Online (Sandbox Code Playgroud)
但由于钥匙是params
你可以传递任意数量的:
var query = fMyTranList.DynamicJoin(fYourTranList,
Tuple.Create("MyAmountGBP", "YourAmountGBP"),
Tuple.Create("AnotherMyTranProperty", "AnotherYourTranProperty"));
Run Code Online (Sandbox Code Playgroud)
实例: http: //rextester.com/AAB2452
归档时间: |
|
查看次数: |
3955 次 |
最近记录: |