Linq 使用 Valueinjecter 选择注入对象

发布于 2024-10-08 08:06:01 字数 586 浏览 2 评论 0原文

我可以将 IQueryable 结果即时转换为注入对象吗?

我知道我可以在 Valueinjecter 的帮助下做到这一点:

usercategory uc1 = new usercategory(example);
usercategoryViewData ucVD1 = new usercategoryViewData();
ucVD1.injectFrom(uc1);

所以而不是这样:

from u in db.usercategories
where u.id==id
select new usercategoryViewModel {id = u.id, name = u.id, description = u.id};

我想使用类似的东西:

from u in db.usercategories
where u.id==id
select new usercategoryViewModel.InjectFrom(u);    

我有 atm 的另一种选择是循环遍历 IEnumerable 并用注入的对象创建一个。

Can I convert a IQueryable result to a injected object on the fly?

I know I can do this with the help of Valueinjecter:

usercategory uc1 = new usercategory(example);
usercategoryViewData ucVD1 = new usercategoryViewData();
ucVD1.injectFrom(uc1);

So instead of this:

from u in db.usercategories
where u.id==id
select new usercategoryViewModel {id = u.id, name = u.id, description = u.id};

I would like to use something like:

from u in db.usercategories
where u.id==id
select new usercategoryViewModel.InjectFrom(u);    

The other alternative I have atm is to loop through a IEnumerable and create one with injected objects instead.

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

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

发布评论

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

评论(2

送舟行 2024-10-15 08:06:02

在这里我展示了两种方法:

        public class Foo
        {
            public string Name { get; set; }            
        }

        [Test]
        public void TestMethod1()
        {
            var bars = new[] { new { Name = "aaa" }, new { Name = "bbb" } };
            IEnumerable<Foo> foos = bars.Select(o => new Foo().InjectFrom(o)).Cast<Foo>();

            IEnumerable<Foo> foos2 = from bar in bars
                        select new Foo().InjectFrom(bar) as Foo;

            Assert.AreEqual(bars.First().Name, foos.First().Name);
            Assert.AreEqual(bars.First().Name, foos2.First().Name);
        }

here I show 2 ways of doing this:

        public class Foo
        {
            public string Name { get; set; }            
        }

        [Test]
        public void TestMethod1()
        {
            var bars = new[] { new { Name = "aaa" }, new { Name = "bbb" } };
            IEnumerable<Foo> foos = bars.Select(o => new Foo().InjectFrom(o)).Cast<Foo>();

            IEnumerable<Foo> foos2 = from bar in bars
                        select new Foo().InjectFrom(bar) as Foo;

            Assert.AreEqual(bars.First().Name, foos.First().Name);
            Assert.AreEqual(bars.First().Name, foos2.First().Name);
        }
薄荷→糖丶微凉 2024-10-15 08:06:02

虽然这可能是可能的,但如果 u 中有任何复杂性,那么我认为这是一个坏主意。

在某些时候,您使用的 ORM(Linq-to-SQL?EF?)需要从在数据库上执行切换到在 .NET 中执行。在该边界上,它需要从数据库中找出需要哪些数据。在第一个示例中,这一点非常清楚:它只需要 u.id。在第二个中,它不知道:它不知道 InjectFrom 将从中读取哪些属性,因此它需要从 UserCategories 表中加载所有值,也许还需要加载相关对象,就在案件。

Whilst that might be possible, if there's any complexity in u then I think it's a bad idea.

At some point the ORM you're using (Linq-to-SQL? EF?) needs to switch from executing on the database to executing in .NET. At that boundary it needs to work out what data it needs from the database. In the first example, that's completely clear: it only needs u.id. In the second it has no idea: it doesn't know what properties InjectFrom will read from it, so it will need to load all the values from the UserCategories table, and maybe related objects too, just in case.

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