- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在试验 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/
我正在使用 jQuery 自动完成。我只想在输入超过 2 位数字时才运行自动完成功能。以下是我尝试过的: $(function() { $('#postcode').on('i
如果我正在做一个,我可以把它作为一个对象返回吗 Select OneItem from Table Where OtherItem = "blah"? 有更好的方法吗? 我正在构建一个构造函数以根据名
我正在创建一个包含上传文件列表的表,我想在最后一列中添加一个链接以允许用户删除相应的文件。我可以简单地使用链接“delete.php?fileid=”,但我更喜欢使用 POST 请求,因此我决定使用一
我已经编写了一个 linq 连接查询,如果其中一个为空,我想获取这些值... 代码: var Details = UnitOfWork.FlightDetails .Query()
我一直在试验 Peter Montgomery 的“Caching the results of LINQ queries”来源列表 here 它为 IQueryable 创建了一个扩展方法返回 IE
错误提示我缺少括号。但是语法与 w3Schools 和 Mozilla 相同。 .css() 在我只设置一种样式时有效,但在设置超过 1 个时会中断。 jQuery $(documen
我在我的实现中使用 Eclipse Equinox 依赖项,当我尝试在 Java 7 中编译时,我收到以下错误。对于 Java 6,它运行完美。 import org.eclipse.equinox.
我是一名优秀的程序员,十分优秀!