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

发布于 2024-11-02 21:50:54 字数 1063 浏览 0 评论 0原文

我有两个列表,帖子和评论。评论具有与帖子列表的查找列,并且帖子与评论列表具有查找(计数相关)关系。我想做的只是显示每个帖子中的评论数量。由于某种原因,我不知道如何使用实体引用来执行此操作。

我有一个 ArchiveItem 类:

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

然后是我尝试运行的查询:

        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()
                         };

我尝试了几种不同的方法并收到各种错误消息。这个特定的版本给了我

查询使用了不受支持的元素,例如对多个列表的引用,或使用 EntityRef/EntitySet 投影完整实体。

有什么想法吗?我以为这很简单,但也许我错过了一些东西。

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:

    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.

Any ideas? I thought this would be pretty simple, but maybe I'm missing something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-11-09 21:50:54

Linq-to-Sharepoint 不支持联接。 sharepoint 列表不是实际数据库中的单独表,sharepoint 的实际数据模型不是重点,但您应该记住,普通 SQL 中的逻辑且廉价的操作本身在 CAML 中并不那么容易,并且每个 Linq-to-Sharepoint 查询最终都会转换为 CAML。

无论如何,连接没有实现。您可以使用查找列的实体来获取数据,但在后台,这始终作为不同的查询实现,根据我的经验,您不能对这些查找实体使用任何聚合或其他多记录操作,包括数数()。

可能有一个很好的方法来解决这个问题,因为 count 是一个非常简单的函数。我会尝试将您想要计数的属性转换为数组(或类似的),并使用它的长度或计数。

一般来说,解决这些问题的方法是在代码中进行数据处理并依赖相当粗略的查询。通过谨慎选择正确的数据结构,您可以很好地加快操作速度。有几次,我体验到代码处理的性能优于 linq-to-sharepoint 查询解决方案的性能,尽管第一种情况下的查询对数据库产生了一定量的不必要的数据流量。

还有一件事:如果您计划最终使用 CAML 或代码生成 Sharepoint 列表,并且您仅在开发过程中使用“单击”内容类型/列表,请记住,SPMetal 在这些情况下生成的类存在差异。更具体地说,查找字段不是表示为实体类,而是表示为两个普通字段,一个是项目 ID,一个是标题(更像是 SPListItem)。此外,反向查找实体集根本不存在。我没有看到这方面的文档,但我经历过。因此,如果您计划使用 CAML 生成的站点,您可能需要重新考虑某些查询。可能有一个解决方法,但根据我的经验,查找实体(集)无论如何都非常慢,最好使用普通的 linq 查询。

我希望这有帮助。

Linq-to-Sharepoint does not support joins. 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.

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().

There is probably a good way around this, because count is such an easy function. 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.

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. 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). 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. 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.

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文