LINQ 到实体转换问题

发布于 2024-12-17 06:45:51 字数 680 浏览 0 评论 0原文

我正在尝试以通用方式过滤 LINQ-to-entities 查询,但我不断收到错误。这是一段代码:

private IQueryable<T> FilterDeletedEntities<T>(IQueryable<T> entities)
{
    if (typeof(IDeletable).IsAssignableFrom(typeof(T)))
    {
        var deletableEntities = (IQueryable<IDeletable>)entities;
        deletableEntities = deletableEntities.Where(entity => !entity.Deleted);
        entities = (IQueryable<T>)deletableEntities;
    }
    return entities;
}

基本上,我试图过滤掉已删除的实体(即“已删除”字段为“true”),当且仅当该实体是 IDeletable(即它具有“已删除”字段)时。问题是我无法强制转换 IQueryable<可删除>返回 IQueryable< T>。

关于如何解决这个问题有什么想法吗?在你问之前:是的,这个方法必须是通用的。

提前致谢!

I'm trying to filter a LINQ-to-entities query in a generic way, but I keep getting an error. Here is a piece of code:

private IQueryable<T> FilterDeletedEntities<T>(IQueryable<T> entities)
{
    if (typeof(IDeletable).IsAssignableFrom(typeof(T)))
    {
        var deletableEntities = (IQueryable<IDeletable>)entities;
        deletableEntities = deletableEntities.Where(entity => !entity.Deleted);
        entities = (IQueryable<T>)deletableEntities;
    }
    return entities;
}

Basically I'm trying to filter out deleted entities (i.e. 'Deleted' field is 'true'), if and only if the entity is IDeletable (i.e. it has the 'Deleted' field). The problem is that I can't cast IQueryable< IDeletable > back to IQueryable< T >.

Any ideas on how to fix this? And before you ask: yes, this method has to be generic.

Thanks in advance!

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

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

发布评论

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

评论(3

浮世清欢 2024-12-24 06:45:51

但您可以使用 Cast() 来转换它。

 entities = deletableEntities.Cast<T>();

您也可以使用它来区分 IDeletable,例如,

private IEnumerable<T> FilterDeletedEntities<T>(IQueryable<T> entities)
{
    if (typeof(IDeletable).IsAssignableFrom(typeof(T)))
    {
        return entities.ToList()
                       .Cast<IDeletable>()
                       .Where( e => !e.Deleted )
                       .Cast<T>();
    }
    return entities.ToList();
}

But you can use Cast<T>() to convert it.

 entities = deletableEntities.Cast<T>();

You could also use it to case to IDeletable as well, for example,

private IEnumerable<T> FilterDeletedEntities<T>(IQueryable<T> entities)
{
    if (typeof(IDeletable).IsAssignableFrom(typeof(T)))
    {
        return entities.ToList()
                       .Cast<IDeletable>()
                       .Where( e => !e.Deleted )
                       .Cast<T>();
    }
    return entities.ToList();
}
花心好男孩 2024-12-24 06:45:51

我能够通过这样做解决我的问题:

private IQueryable<T> FilterDeletedEntities<T>(IQueryable<T> entities)
{
    if (typeof(IDeletable).IsAssignableFrom(typeof(T)))
    {
        var deletableEntities = (IQueryable<IDeletable>)entities;
        return deletableEntities.Where(entity => !entity.Deleted).Cast<T>();
    }
    return entities;
}

感谢 tvanfosson 的灵感。

I was able to solve my problem by doing this:

private IQueryable<T> FilterDeletedEntities<T>(IQueryable<T> entities)
{
    if (typeof(IDeletable).IsAssignableFrom(typeof(T)))
    {
        var deletableEntities = (IQueryable<IDeletable>)entities;
        return deletableEntities.Where(entity => !entity.Deleted).Cast<T>();
    }
    return entities;
}

Thanks to tvanfosson for the inspiration.

你是年少的欢喜 2024-12-24 06:45:51

如果您可以假设没有人需要使用未实现 IDeletable 的 T 调用此方法,则可以限制 T:

private IQueryable<T> FilterDeletedEntities<T>(IQueryable<T> entities) where T : IDeletable

作为奖励,您不需要强制转换任何内容或使用反射来测试 IDeletable。

If you can assume that no one will need to call this method with T that does not implement IDeletable, you can restrict T:

private IQueryable<T> FilterDeletedEntities<T>(IQueryable<T> entities) where T : IDeletable

As a bonus, you won't need to cast anything or use reflection to test for IDeletable.

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