简体   繁体   English

Linq查询中的SharePoint查找(与计数相关)值

[英]SharePoint Lookup (Count Related) value in Linq Query

I have two lists, Posts and Comments. 我有两个列表,帖子和评论。 Comments has a Lookup column to the Posts list, and the Posts has a Lookup (Count Relate) relationship back to the Comments list. 评论在“帖子”列表中具有一个“查找”列,而“帖子”具有返回到“评论”列表的“查找(计数相关)”关系。 What I'm trying to do is just display the number of Comments in each Post. 我想做的只是在每个帖子中显示评论数。 For some reason I can't get how to do this with the Entity References. 出于某种原因,我无法使用实体引用执行此操作。

I have an ArchiveItem class: 我有一个ArchiveItem类:

    public class ArchiveItem
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public string Comments { get; set; }
        public string Date { get; set; }
    }

And then the query that I'm trying to run: 然后我要运行的查询:

        var queryItems = from item in spotlightItems
                         join comment in commentItems on item.Title equals comment.Title
                         select new ArchiveItem
                         {
                             Id = item.Id.ToString(),
                             Title = item.Title,
                             Comments = comment.Post.Title.Count().ToString(),
                             Date = item.Date.ToString()
                         };

I've tried a few different ways and get a variety of error messages. 我尝试了几种不同的方法,并收到各种错误消息。 This particular version gives me 这个特殊的版本给我

The query uses unsupported elements, such as references to more than one list, or the projection of a complete entity by using EntityRef/EntitySet. 该查询使用不受支持的元素,例如对多个列表的引用,或者通过使用EntityRef / EntitySet投影整个实体。

Any ideas? 有任何想法吗? I thought this would be pretty simple, but maybe I'm missing something. 我以为这很简单,但也许我遗漏了一些东西。

Linq-to-Sharepoint does not support joins. Linq-to-Sharepoint不支持联接。 A sharepoint list is not a separate table in the actual database, the actual data model of sharepoint is not the point, but you should keep in mind that what's a logical and cheap operation in plain SQL is not that easy per se in CAML, and every Linq-to-Sharepoint query is ultimately converted to CAML. 共享点列表不是实际数据库中的单独表,共享点的实际数据模型也不是重点,但是您应该记住,在CAML中,逻辑上廉价的操作本身在CAML中并不是那么容易,并且每个Linq-to-Sharepoint查询最终都会转换为CAML。

Anyhow, joins aren't implemented. 无论如何,联接没有实现。 You can use the lookup columns' Entity to get the data, but in the background this is always implemented as a different query, and in my experience you cannot use any aggregate or other multi-record operations on these lookup'd-entities, including Count(). 您可以使用查阅列的Entity来获取数据,但是在后台,这总是作为不同的查询来实现的,以我的经验,您不能对这些查阅实体使用任何聚合或其他多记录操作,包括计数()。

There is probably a good way around this, because count is such an easy function. 解决这个问题的方法可能不错,因为count是一个简单的功能。 I would try converting the property you want to count to an array (or similar), and using the length or count of that. 我会尝试将要计数的属性转换为数组(或类似数组),并使用其长度或计数。

In general, the way around these issues is to do the data processing in your code and rely on fairly crude queries. 通常,解决这些问题的方法是在代码中进行数据处理并依赖相当粗略的查询。 And by investing some care in choosing the correct data structures you can speed up the operations really well. 通过花一些心思选择正确的数据结构,您可以真正加快操作速度。 On several occasions I experienced better performance with code-processing then with linq-to-sharepoint query solutions, even though the queries in the first case produced a certain amount of unnecessary data traffic to the database. 在某些情况下,即使在第一种情况下,查询产生了到数据库的一定数量不必要的数据通信,我在代码处理方面的性能都比在linq-to-sharepoint查询解决方案中的性能要好。

One more thing: If you plan to eventually generate your Sharepoint Lists using CAML or code, and you are using 'clicked' content-types/lists only during development, bear in mind that there are differences in classes SPMetal generates in these cases. 还有一件事:如果您打算最终使用CAML或代码生成共享点列表,并且仅在开发过程中使用“单击”内容类型/列表,请记住在这些情况下SPMetal生成的类有所不同。 More specifically, lookup fields are represented not as Entity classes, but as two normal fields, on with the item Id and one with the title (more like in SPListItem). 更具体地说,查找字段不是表示为Entity类,而是表示为两个普通字段,分别带有项目ID和一个带有标题(更像SPListItem)。 Also, the reverse lookup-entitysets are not present at all. 而且,反向查找实体集根本不存在。 I've not seen documentation about this, but I have experienced it. 我还没有看到有关此文档,但是我已经经历了。 Consequently, you may need to rethink some of you queries if you plan to use a CAML generated site. 因此,如果您打算使用CAML生成的网站,则可能需要重新考虑一些查询。 There may be a workaround, but in my experience Lookup Entity(sets) are very slow anyway, and it's better to use a normal linq query. 可能有解决方法,但以我的经验,无论如何,查找实体(集合)都非常慢,因此最好使用普通的linq查询。

I hope this helps. 我希望这有帮助。

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

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