我在其他答案中读到了如何在运行时更改 linq 查询的参数。但是,在创建查询之后,是否可以更改查询浏览的数据源?也许可以为查询提供一个空包装器,可以在查询不知情的情况下插入和拔出数据源?
例如,让我们假设这种情况:
// d1 is a dictionary
var keys = from entry in d1
where entry.Value < 5
select entry.Key;
现在,让我们假设我希望 myQuery 保持不变,而不是修改 d1,除非我希望它处理全新的字典 d2。
这样做的原因是我想将谁提供查询与谁提供数据源分离。 IE。将字典视为与服务关联的元数据,将查询视为服务使用者发现哪组服务符合其条件的手段。我需要对每个服务的元数据应用相同的查询。
我想我(可能是错误的)所做的一个相似之处是正则表达式:可以编译正则表达式,然后将其应用于任何字符串。我想对查询和字典做同样的事情。
最佳答案
LINQ 查询实际上只不过是方法调用。因此,您无法在设置后“重新分配”已调用方法的对象。
您可以通过创建一个包装类来模拟这一点,该类允许您更改实际枚举的内容,即:
public class MutatingSource<T> : IEnumerable<T>
{
public MutatingSource(IEnumerable<T> originalSource)
{
this.Source = originalSource;
}
public IEnumerable<T> Source { get; set; }
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return Source.GetEnumerator();
}
}
这将允许您创建查询,然后在事后“改变主意”,即:
var someStrings = new List<string> { "One", "Two", "Three" };
var source = new MutatingSource<string>(someStrings);
// Build the query
var query = source.Where(i => i.Length < 4);
source.Source = new[] {"Foo", "Bar", "Baz", "Uh oh"};
foreach(var item in query)
Console.WriteLine(item);
这将打印 Foo
、Bar
和 Baz
(来自“已更改”的源项目)。
编辑回应评论/编辑:
I guess one parallel that I'm (maybe erroneously) making is with Regular Expression: one can compile a regular expression and then apply it to any string. I'd like to do the same with queries and dictionaries.
在这种情况下,查询与正则表达式不同。查询被直接转换为一系列方法调用,这仅适用于底层源。因此,您无法更改该源(没有“查询对象”,只有方法调用的返回值)。
更好的方法是将查询移至方法中,即:
IEnumerable<TKey> QueryDictionary<TKey,TValue>(IDictionary<TKey,TValue> dictionary)
{
var keys = from entry in dictionary
where entry.Value < 5
select entry.Key;
return keys;
}
然后您可以根据需要使用它:
var query1 = QueryDictionary(d1);
var query2 = QueryDictionary(d2);
关于c# - 如何交换与 Linq 查询关联的数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19306987/