简体   繁体   English

Linq随机返回周期性的空结果

[英]Linq randomly returns periodic null results

I've got a linq query, which is admittedly pretty large. 我有一个linq查询,它肯定很大。 I can't identify where it is going wrong because it ONLY happens on the remote server and I don't have the access to debug it. 我无法确定问题出在哪里,因为它仅在远程服务器上发生,并且我无权对其进行调试。 But it is basically like this... 但是基本上就是这样...

This code lists all of the 'folders' that the current member can see. 这段代码列出了当前成员可以看到的所有“文件夹”。 (Folders contain images.) (文件夹包含图像。)

            if (membership == null || membership.Count() == 0)
                membership = new string[] { "Guests" };

            return DataContext.Folders.Where(f => f.Ring.Keys.Any(k => k.Tag.Id == id))
                .Where(i => i.Ring.Keys.Any(t => membership.Contains(t.Tag.Name))).OrderBy(n => n.Date).DefaultIfEmpty();

This code lists all of the 'tags' the user can see (tags are contained in rings, which can be hooked onto folders or images) 此代码列出了用户可以看到的所有“标签”(标签包含在环中,可以挂在文件夹或图像上)

        IQueryable<Models.Tag> tags = null;
        do
        {
            DataContext = new Models.Contexts.DatabaseDataContext();

            tags = null;
            if (membership == null)
                membership = new string[] { "Guests" };

            tags = DataContext.Tags.Where(t => t.Keys.Any(k => k.Ring.Name == category))
                .Where(t => t.Keys.Any(k => k.Ring.Keys.Any(c => membership.Contains(c.Tag.Name)))).OrderBy(o => o.Name);
        }
        while (tags == null || tags.Count() == 0);

        return tags;

I enclosed it in a do loop, much to my dismay, to see if it could force it to actually keep calling until it was no longer null. 我非常沮丧地将它封装在一个do循环中,以查看它是否可以强制它继续调用直到不再为空。 No such luck, It still comes back empty. 没有这种运气,它仍然回到空虚。 Again, this EXACT same code works 'sometimes', and 100% of the time on EVERY test back, querying the SAME database. 同样,此完全相同的代码有时“起作用”,并且100%的时间在每次重新测试时都查询SAME数据库。 I have even tried different web hosts - all with the same failures once it is live. 我什至尝试了不同的Web主机-上线后所有主机均出现相同的故障。

membership is a string[] that contains a list of all of the roles the member belongs to. 成员资格是一个string [],其中包含该成员所属的所有角色的列表。

Folder
- Id
- RingId

Ring
- Id
- Name (Nullable)

Tag
- Id
- Name

Key
- Id
- RingId
- TagId

Image
- Id
- FolderId
- RingId

This is the basic database structure setup. 这是基本的数据库结构设置。

Now, this query runs fine on my local machine - in all of my tests, etc. But when I put it live, it just randomly starts returning null sometimes . 现在,此查询可以在我的本地计算机上正常运行-在所有测试中,等等。但是当我将其投入使用时, 有时它只是随机开始返回null。 I cannot find a rhyme or a reason to why, it runs fine for a few requests, then it just stops returning the results. 我找不到韵律或原因,它可以很好地处理一些请求,然后停止返回结果。

The result set it returns is pretty large. 它返回的结果集非常大。 I'd say about 880 items. 我说大约880项。 And the amount of times it is accessed per second is very, very high. 而且每秒访问它的次数非常非常多。 I thought at first maybe it was just the stress of how many people were accessing it. 我以为一开始也许只是因为有多少人在使用它。

Is there any information I can provide that might help in debugging this? 我是否可以提供任何信息来帮助调试此信息? There are a few other queries similar to this one - I have tried, and tried, and tried, and I simply cannot reproduce the results in a debugger. 还有其他一些与此查询类似的查询-我已经尝试过,并且尝试过,并且尝试过,但我根本无法在调试器中重现结果。 I am getting all sorts of InvalidCastException calls - but there's never any casting. 我收到各种各样的InvalidCastException调用-但从来没有任何强制转换。 Everything is being returned as IQueryable - The views do not do anything special except take in Guids and pass the Membership data from the ASP.NET Membership Provider - which I've checked for and confirmed that it is working. 一切都以IQueryable的形式返回-这些视图除了接受Guid并传递来自ASP.NET Membership Provider的Membership数据以外,没有做任何特殊的事情-我已经检查并确认它正在工作。 (Inserted forced data, instead of letting the provider do it - still fails) (插入强制数据,而不是让提供程序执行-仍然失败)

I will try to provide any information needed, but I am really getting frustrated - as none of this is making sense to me (why it would be failing). 我将尝试提供所需的任何信息,但我真的感到沮丧-因为这对我来说都不有意义(为什么会失败)。

Thank you very much for your time. 非常感谢您的宝贵时间。 (This is all done in .NET 3.5, ASP.NET MVC 1.0, C# ) - the objects were created using LINQ to SQL. (这全部在.NET 3.5,ASP.NET MVC 1.0,C#中完成)-这些对象是使用LINQ to SQL创建的。

This really sounds like a load issue. 这听起来确实像是负载问题。 Put some perf counters on the web server and watch it for usage. 在Web服务器上放置一些性能计数器,并观察其使用情况。

Ok I'm not sure if this is your problem but it's too hard to write it back in a comment, 好的,我不确定这是否是您的问题,但是很难在评论中写回,

Your data context is disposable, assuming you're using LINQ to SQL. 假设您正在使用LINQ to SQL,则数据上下文是可抛弃的。 Therefore while you want to use it you can wrap it in a using statement like so: 因此,当您要使用它时,可以将其包装在using语句中,如下所示:

    do
    {
        using (DataContext = new Models.Contexts.DatabaseDataContext())
        {

            tags = null;
            if (membership == null)
                membership = new string[] { "Guests" };

            tags = DataContext.Tags.Where(t => t.Keys.Any(k => k.Ring.Name == category))
                .Where(t => t.Keys.Any(k => k.Ring.Keys.Any(c => membership.Contains(c.Tag.Name)))).OrderBy(o => o.Name);
        }
    }

This way it will dispose your connection after every attempt to use it, otherwise it will remain in memory for some period of time. 这样,它将在每次尝试使用连接后处理您的连接,否则它将在内存中保留一段时间。 Because this period of time is up to the garbage collector, it could be the reason why it works some times and not others. 因为这段时间取决于垃圾收集器,所以这可能是它有时起作用而不是其他时间起作用的原因。 Try it and let me know if it fixed your problem. 试试看,让我知道它是否解决了您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM