如何在 EntityFramework 和 ESQL 中根据子属性从父级中选择

发布于 2024-10-19 12:45:40 字数 701 浏览 1 评论 0原文

我在实体框架中有一个父/子表,我需要根据子主键从父项中选择一些行。

我编写了这些代码:

List<int> s = (from all in DB.TbProfiles
                    where all.TbMaharat.Any(c => maharat.Contains(c.MaharatId))
                    select all.ProfileId).ToList();

但我发现 Linq2Entity 不支持“Contains”,我必须使用“MultiSet”和 ESQL 命令。但我可以找到任何样本来做到这一点。

我这样写ESQL,但它不起作用:

byte[] moshTypes = new byte[] { 1, 2, 3 };

        List<int> s =  DB.TbProfiles.Where("it.TbMaharat exists(Select 0 from TbMaharat as e where e.MaharatId IN MultiSet (" + string.Join(",", moshTypes) + "))")
            .Select(c=>c.ProfileId).ToList();

        return s;

任何人都可以帮助我吗?

I have a parent/child table in Entity Framework and I need to select some row from parent based on child primary key.

I write these code:

List<int> s = (from all in DB.TbProfiles
                    where all.TbMaharat.Any(c => maharat.Contains(c.MaharatId))
                    select all.ProfileId).ToList();

but I Found that Linq2Entity does not support "Contains", and I must use "MultiSet" and ESQL command. but i can find any sample to do this.

I write ESQL like this but it does not work:

byte[] moshTypes = new byte[] { 1, 2, 3 };

        List<int> s =  DB.TbProfiles.Where("it.TbMaharat exists(Select 0 from TbMaharat as e where e.MaharatId IN MultiSet (" + string.Join(",", moshTypes) + "))")
            .Select(c=>c.ProfileId).ToList();

        return s;

Can anyone help me?

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

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

发布评论

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

评论(1

桃气十足 2024-10-26 12:45:41

要使用 IEnumerable.Contains() 方法,您必须使用 ToList()ToArray() 将表加载到内存中。然而,这可能会导致巨大的内存成本,因此除非表非常小并且不会及时增长,否则不要这样做。
Contains 方法有一个解决方法:为每个值构建一个 OR 表达式,以便 Contains() 方法变为 DB.TbMaharat。其中(x=>x.MaharatId == 1 || x.MaharatId == 2 || x.MaharatId == 3)。有关如何执行此操作的信息,请参阅 MSDN 线程: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/

To use IEnumerable<T>.Contains() method you'd have to load the table into memory using ToList() or ToArray(). However this can lead to great memory cost so just don't do it unless the table is very small and won't grow in time.
There is a workaround for Contains method: build an OR expression for each value so the Contains() method becomes DB.TbMaharat.Where(x=>x.MaharatId == 1 || x.MaharatId == 2 || x.MaharatId == 3). See the MSDN thread on how to do this: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/ .

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