针对映射对象运行 LINQ where 子句

发布于 2024-09-18 00:47:34 字数 669 浏览 4 评论 0 原文

如果我想

BLL.Person person = (BLL.Person)repository.Single(item => item.Id == Id);

在我的单一方法中运行类似 Down 的东西,我会这样做:

public Resource Single(Expression<Func<BLL.Resource, bool>> where)
{
     Resource resource = AsQueryable().FirstOrDefault(where);
     return resource;
}

protected IQueryable<BLL.Resource> AsQueryable()
{
     // I need to use the where clause on an object called DAL.Resource
     throw new NotImplementedException();
}

DAL.Resource 对象与 BLL.Resource 相同,但是 BLL 副本不知道持久性。我可以使用 automapper 映射事物返回我想要的集合没有问题,但是我需要 where 子句运行 DAL 而不是 BLL...

这一定是可能的!任何想法将不胜感激。

If I want to run something like this

BLL.Person person = (BLL.Person)repository.Single(item => item.Id == Id);

Down in my single method I'd do something like this:

public Resource Single(Expression<Func<BLL.Resource, bool>> where)
{
     Resource resource = AsQueryable().FirstOrDefault(where);
     return resource;
}

protected IQueryable<BLL.Resource> AsQueryable()
{
     // I need to use the where clause on an object called DAL.Resource
     throw new NotImplementedException();
}

The DAL.Resource object is identical to the BLL.Resource, however the BLL copy is ignorant of persistence.. I can map things using automapper no problem to return a collection of what I want, however I need the where clause to run agaisnt DAL not BLL...

This must be possible somehow! Any ideas would be appreciated.

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

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

发布评论

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

评论(2

纸短情长 2024-09-25 00:47:34

您对 where 子句的签名 (Expression> where) 是通往答案的非常好的一步。添加 Expression<...> 告诉 C# 编译器为 where 参数创建表达式树而不是委托。解决方案是遍历表达式树,并将对 BLL.Person 的所有引用替换为对 DAL.Person 的引用。正如您所说,既然它们是相同的,那么您应该能够针对 DAL 编译和运行修改后的表达式树,没有任何问题。

Your signature for the where-clause (Expression<Func<BLL.Resource, bool>> where) is a very good step along the way to the answer. Adding Expression<...> tells the C# compiler to create an expression tree rather than a delegate for the where parameter. The solution is to traverse the expression tree and replace all references to your BLL.Person with references to DAL.Person. Since they are identical, as you say, then you should be able to compile and run the modified expression tree against the DAL without any problems.

明月夜 2024-09-25 00:47:34

这可能有点离谱,但我就是这样做的。在我的 BLL 对象中,我在构造函数中传递了一个 DAL 对象。在我的服务中,我获取 DAL 对象,创建一个新的 BLL 对象,并将 DAL 对象传入。然后 BLL 对象映射出 DAL 对象。

using PersonDto = DAL.IPerson;

namespace BLL
{
    public class Person : IPerson
    {

        private readonly PersonDto _person;

        public Person(PersonDto person)
        {
            _person = person;
        }

        public string Name
        {
            get { return _person.Name; }
        }
    }
}

然后使用 LINQ 执行此操作:

BLL.Person person = new Person(repository.Single(item => item.Id == Id));

可能有点令人困惑,如果我偏离基地,请告诉我,或者您需要更多解释。

This may be way off base, but this is how I do it. In my BLL objects, I pass a DAL object in in the constructor. In my services, I get the DAL object, and create a new BLL object, and pass the DAL object in. The BLL object then maps out the DAL object.

using PersonDto = DAL.IPerson;

namespace BLL
{
    public class Person : IPerson
    {

        private readonly PersonDto _person;

        public Person(PersonDto person)
        {
            _person = person;
        }

        public string Name
        {
            get { return _person.Name; }
        }
    }
}

Then do this with LINQ:

BLL.Person person = new Person(repository.Single(item => item.Id == Id));

Probably a little confusing, let me know if I am way off base, or you need more explanation.

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