gpt4 book ai didi

c# - 从缓存列表中查询 IQuery 返回 null 异常

转载 作者:行者123 更新时间:2023-11-30 21:17:20 24 4
gpt4 key购买 nike

我一直在试验 Peter Montgomery 的“Caching the results of LINQ queries”来源列表 here

它为 IQueryable<T> 创建了一个扩展方法返回 IEnumerable<T>如果可能,从缓存数据中获取。主要的扩展方法如下所示。

    /// <summary>
/// Returns the result of the query; if possible from the cache, otherwise
/// the query is materialized and the result cached before being returned.
/// </summary>
/// <param name="query">The IQueryable for which to return the query.</param>
/// <param name="priority">The relative cache priority of the object.</param>
/// <param name="slidingExpiration">The timespan indicating the duration of the sliding expiration</param>
/// <returns>The result of the query; if possible from the cache</returns>
/// <typeparam name="T">The type of entity for which to provide the method.</typeparam>
public static IEnumerable<T> FromCache<T>(this IQueryable<T> query, CacheItemPriority priority, TimeSpan slidingExpiration)
{
string key = query.GetCacheKey();

// try to get the query result from the cache
var result = HttpRuntime.Cache.Get(key) as List<T>;

if (result == null)
{
// TODO: ... ensure that the query results do not
// hold on to resources for your particular data source
//
//////// for entity framework queries, set to NoTracking
//////var entityQuery = query as ObjectQuery<T>;
//////if (entityQuery != null)
//////{
////// entityQuery.MergeOption = MergeOption.NoTracking;
//////}

// materialize the query
result = query.ToList();

HttpRuntime.Cache.Insert(
key,
result,
null, // no cache dependency
Cache.NoAbsoluteExpiration,
slidingExpiration,
priority,
null); // no removal notification
}

return result;
}

我使用的方法是这样的:

    /// <summary>
/// Retrieves all instances of the specified type, if possible from the cache.
/// Objects are maintained in a <see cref="T:System.Data.EntityState.Detached">Detached</see> state and
/// are not tracked in the <see cref="T:System.Data.Objects.ObjectStateManager">ObjectStateManager</see>.
/// </summary>
/// <returns>A list of all instances of the specified type.</returns>
/// <typeparam name="T">The type of entity for which to provide the method.</typeparam>
public IQueryable<T> All<T>() where T : class, new()
{
//return new ObjectQuery<T>(GetSetName<T>(), this.context, MergeOption.NoTracking);
return new ObjectQuery<T>(GetSetName<T>(), this.context, MergeOption.NoTracking).FromCache<T>().AsQueryable<T>();
}

然后我会像这样过滤我的请求(查询 AdventureWorks 数据库示例):

List<Product> products = new List<Product>(readonlySession.All<Product>()
.Where(x => x.Color.Equals("Black", StringComparison.InvariantCultureIgnoreCase)));

我的问题是,当我尝试像这样查询数据时,我得到一个 NullReferenceException因为某些产品会为属性 Color 返回 null .如果我在没有缓存的情况下查询实体,则没有问题。

谁能解释为什么会发生这种情况以及我该如何解决这个问题?

非常感谢。

更新:我发现使用 == 解决了我的问题,尽管我不知道为什么。不过,我希望能够使用 Equals()。

最佳答案

当您执行 x.Color.Equals("Black", StringComparison.InvariantCultureIgnoreCase) 并且 Color 为 null 时,您将得到 null 异常,更好的方法是使用静态字符串方法,即使参数为 null 也能正常工作,如下所示:

String.Equals(x.Color, "Black", StringComparison.InvariantCultureIgnoreCase)

关于c# - 从缓存列表中查询 IQuery 返回 null 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4844433/

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