如何在 EntityFramework 和 ESQL 中根据子属性从父级中选择
我在实体框架中有一个父/子表,我需要根据子主键从父项中选择一些行。
我编写了这些代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用
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 usingToList()
orToArray()
. 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 anOR
expression for each value so theContains()
method becomesDB.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/ .