获取 IQueryable的奇怪问题在 foreach 循环中工作的集合

发布于 2024-11-05 19:16:40 字数 1794 浏览 0 评论 0原文

我正在构建一个存储库方法(实体框架)来接收由表单中的复选框提供的 id 集合作为 CMS 的一部分,并更新将主题与出版物相关联的查找表(实体集)。

我在存储库中有这个方法:

public void AttachToTopics(int pubId, IQueryable<int> topicsForAssociation, IQueryable<int> topicsSubset, int primaryTopicId)

{

    // EVERYTHING IS FINE IF I INSERT A MANUAL COLLECTION  OF int LIKE THIS:
    // var priorAssociatedTopics = new[] { 2 }.AsQueryable();  // 

    // BUT WHAT I REALLY NEED TO WORK IS THIS:  
    IQueryable<int> priorAssociatedTopics = ListTopicIdsForPublication(pubId);

    var priorAssociatedTopicsToExamine = priorAssociatedTopics.Intersect(topicsSubset);

    var topicsToAdd =
        associatedTopics.Intersect(topicsSubset).Except(priorAssociatedTopicsToExamine);

    foreach (var topicToAdd in topicsToAdd)
        AttachToTopic(pubId, topicToAdd);


    foreach (var topicToRemove in priorAssociatedTopicsToExamine.Except(associatedTopics))
        DetachFromTopic(pubId, topicToRemove);

}

AttachToTopics 在第一个 foreach 循环上阻塞,产生此错误消息:

This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code.

但问题实际上出在第一行:该行上调用的存储库方法将适当类型的集合提供到 previousAssociatedTopics 中,而 Intellisense 没有显式将其键入为 IQueryable 会出现问题(通常,我会使用 var),并且调试器显示该变量包含整数集合。

public IQueryable<int> ListTopicIdsForPublication(int pubId)
{
    var topics = from x in DataContext.TopicPublications where x.PublicationId == pubId  select x;
    return topics.Select(t => t.Id);
}

然而,回到attachToTopics,我的topicsToAdd 集合没有被填充,并且其调试中的结果视图包含上述错误消息。

奇怪的是,如果我为 previousAssociatedTopics 切换手动生成的 IQueryable 整数集合(请参阅上面代码中的注释),则 foreach 循环工作正常。所以我相信我需要找到一些其他方法来从我的存储库中的方法调用中获取用整数填充的priorAssociatedTopics。

有什么线索吗?

I'm building a repository method (Entity Framework) to take in a collection of ids supplied by checkboxes in a form as part of a CMS, and updating a lookup table (entity set) that relates topics to publications.

I have this method in a respository:

public void AttachToTopics(int pubId, IQueryable<int> topicsForAssociation, IQueryable<int> topicsSubset, int primaryTopicId)

{

    // EVERYTHING IS FINE IF I INSERT A MANUAL COLLECTION  OF int LIKE THIS:
    // var priorAssociatedTopics = new[] { 2 }.AsQueryable();  // 

    // BUT WHAT I REALLY NEED TO WORK IS THIS:  
    IQueryable<int> priorAssociatedTopics = ListTopicIdsForPublication(pubId);

    var priorAssociatedTopicsToExamine = priorAssociatedTopics.Intersect(topicsSubset);

    var topicsToAdd =
        associatedTopics.Intersect(topicsSubset).Except(priorAssociatedTopicsToExamine);

    foreach (var topicToAdd in topicsToAdd)
        AttachToTopic(pubId, topicToAdd);


    foreach (var topicToRemove in priorAssociatedTopicsToExamine.Except(associatedTopics))
        DetachFromTopic(pubId, topicToRemove);

}

AttachToTopics chokes on the first foreach loop, yielding this error message:

This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code.

But the problem is really with the first line: the repository method called on that line provides the appropriately typed collection into priorAssociatedTopics, and Intellisense has no problem with explicitly typing it as IQueryable (normally, I'd use var), and the debugger shows that this variable holds a collection of integers.

public IQueryable<int> ListTopicIdsForPublication(int pubId)
{
    var topics = from x in DataContext.TopicPublications where x.PublicationId == pubId  select x;
    return topics.Select(t => t.Id);
}

However, back in attachToTopics, my topicsToAdd collection doesn't get populated, and its Results View in debug holds the aforementioned error message.

Curiously, if I switch in a manually generated IQueryable collection of ints for priorAssociatedTopics (see comment in code, above) the foreach loop works fine. So I beleive I need to find some other way to get priorAssociatedTopics populated with ints from a method call in my repository.

Any clue out there?

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

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

发布评论

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

评论(1

说谎友 2024-11-12 19:16:40

在这种情况下,ListTopicIdsForPublication 无法返回 IEnumerableIList 是否有任何原因?如果是这样,在 topics.Select(t => t.ID) 末尾添加 .ToList() 将确保查询在此时运行。

问题的根源并不是 IQueryable,而是来自 DataContext.TopicPublicationsIQueryable。看起来它正在丢失上下文数据信息,这就是您收到异常的原因。

Is there any reason that ListTopicIdsForPublication can't return an IEnumerable<int> or an IList<int> in this case? If so, adding a .ToList() at the end of topics.Select(t => t.ID) will ensure that the query gets run at that point.

It's not so much that it's IQueryable<int> that's causing the problem, but rather IQueryable<int> from DataContext.TopicPublications. It looks like it's losing it's contextual data information and that's why you're getting the exception.

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