2008-11-05 33 views
6

我有一个LINQ查询,我试图把在一个序列化对象的分布式缓存(速度),但其未能由于LINQ到SQL懒列表我将如何序列化LINQ到SQL懒惰名单

像这样

return from b in _datacontext.MemberBlogs 
        let cats = GetBlogCategories(b.MemberBlogID) 
        select new MemberBlogs 
        { 
         MemberBlogID = b.MemberBlogID, 
         MemberID = b.MemberID, 
         BlogTitle = b.BlogTitle, 
         BlogURL = b.BlogURL, 
         BlogUsername = b.BlogUsername, 
         BlogPassword = b.BlogPassword, 
         Categories = new LazyList<MemberBlogCategories>(cats) 
        }; 

LazyList是同一类罗布科纳使用他的MVC店面...

所有三个类被标记序列化(MemberBlogs,MemberBlogCategories,LazyList任何...想法?

回答

6

如果你把它在一个分布式缓存,你需要完全避免了LazyList。然后,您可以调用.ToList()围绕整个LINQ语句中:

(from x select new MemberBlogs).ToList() 

这应该然后是可缓存的,因为它会强制评估的查询。

2

如果你为什么要使用一个懒惰的名单缓存呢?不要使用懒惰列表,使用缓存,问题就会消失。

3

我只是猜测,但我要说的问题是,它是序列化的查询,而不是结果;我不知道是什么LazyList实施的样子,但你也许可以增加实际执行之前,其序列查询的OnSerializing方法;喜欢的东西:

[OnSerializing] 
private void ExecuteLinqQuery(StreamingContext context) 
{ 
    if (!SomethingThatIndicatesThisLinqQueryHasNotBeenExecuted) 
     LinqVariable.ToList() 
} 

这样你能留住延迟加载(对于任何不进入缓存),但随后又如果它击中缓存,它会执行LINQ查询和缓存结果。

0

我知道这是一个旧帖子,但我有同样的问题,因为我想执行我的LazyList并将它们放到AppFabric缓存中。我最终将一些自定义序列化逻辑放入LazyList类型中。

第一部分现在看起来是这样的:

public class LazyList<T> : IList<T>, ISerializable 
{ 

    public LazyList() 
    { 
     this.query = new List<T>().AsQueryable(); 
    } 

    public LazyList(SerializationInfo info, StreamingContext context) 
    { 
     try { 
      this.inner = (List<T>)info.GetValue("InnerList", typeof(List<T>)); 
     } 
     catch (Exception ex) 
     { 
      this.inner = null; 
     } 
    } 

    public void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     if (this.inner != null) 
      info.AddValue("InnerList", this.inner.ToList()); 
    } 

    public LazyList(IQueryable<T> query) 
    { 
     this.query = query; 
    } 

    public LazyList(List<T> l) 
    { 
     inner = l; 
    } 
}