首页 \ 问答 \ 为什么我的实体框架创建了多个查询而不是单个UNION查询?(Why my Entity Framework creates several queries instead of a single UNION query?)

为什么我的实体框架创建了多个查询而不是单个UNION查询?(Why my Entity Framework creates several queries instead of a single UNION query?)

我使用Entity Framework Core 2.0.1。 在我的EF查询中,我需要将几个简单查询组合到一个UNION查询中。 所以,对于一个非常基本的例子,我有几个类似的查询

using (var dbc = new ItemsDbContext("some-connection-string"))
{
    var q1 = dbc.Items.Where(a => a.ItemType == "A");
    var q2 = dbc.Items.Where(a => a.ItemType == "B");

    var myList = q1.Union(q2).ToList();
}

我希望这个SQL语句作为单个查询对我的SQL Server数据库执行:

SELECT I.*
    FROM [dbo].[Items]
    WHERE I.ItemType = N'A'
UNION
SELECT I.*
    FROM [dbo].[Items]
    WHERE I.ItemType = N'B'

但是,正如我在SQL Server Profiler中看到的那样,执行了两个单独的查询:

查询1:

SELECT I.*
    FROM [dbo].[Items]
    WHERE I.ItemType = N'A'

查询2:

SELECT I.*
    FROM [dbo].[Items]
    WHERE I.ItemType = N'B'

这两个单独查询的结果稍后由实体框架联合。 但它是在调用代码中完成的,而不是在SQL Server端。

如何让Entity Framework Core生成UNION而不是多个查询?


I use Entity Framework Core 2.0.1. In my EF query I need to combine several simple queries into a single UNION query. So, for a very basic example, I have a couple of similar queries

using (var dbc = new ItemsDbContext("some-connection-string"))
{
    var q1 = dbc.Items.Where(a => a.ItemType == "A");
    var q2 = dbc.Items.Where(a => a.ItemType == "B");

    var myList = q1.Union(q2).ToList();
}

I would like such SQL statement to be executed, as a single query, against my SQL Server database:

SELECT I.*
    FROM [dbo].[Items]
    WHERE I.ItemType = N'A'
UNION
SELECT I.*
    FROM [dbo].[Items]
    WHERE I.ItemType = N'B'

However, as much as I see in SQL Server Profiler, two separate queries are executed:

Query 1:

SELECT I.*
    FROM [dbo].[Items]
    WHERE I.ItemType = N'A'

Query 2:

SELECT I.*
    FROM [dbo].[Items]
    WHERE I.ItemType = N'B'

Result of this two separate queries is later union'ed by Entity Framework. But it is done in the calling code, not on SQL Server's side.

How can I make Entity Framework Core generate UNION instead of several queries?


原文:https://stackoverflow.com/questions/49597645
更新时间:2024-04-27 20:04

最满意答案

Com_config管理该视图,它非常期望标准结构。 只要不破坏其他扩展,您就可以为它执行布局覆盖。 例如,您可以将该选项作为您的扩展条件。


Com_config manages that view and it pretty much expects a standard structure. You might be able to do a layout override for it as long as you don't break the other extensions. You could for example make it conditional on the option being your extension.

相关问答

更多
  • 您需要创建自定义字段类型,您可以在其中实现所需的所有逻辑。 如果您需要使用组件存储值,请添加隐藏的输入字段,并使用javascript在加载时填充标记,并在用户交互中插入要存储的值(还可以存储以json编码的对象) 。 Joomla将负责保存和检索它。 文档 You need to create a custom field type, where you'll be able to implement all the logic that's needed. In case you need to sto ...
  • HTTP请求方法GET与URL一起工作,因此变量总是在请求的URL中传递。 要将q添加到当前网址,只需添加&q=SomeValue ,其中SomeValue已适当地进行百分比或URL编码。 Joomla 1.5 如果你使用Joomla! 1.5您可以使用JRequest获取任何变量的值(无论是通过POST还是GET提交),请参阅此文档以获取请求变量 。 $q = JRequest::getVar('q'); Joomla 1.6+ 对于Joomla! JInput推荐使用JInput来检索请求数据,因为J ...
  • 您可以调用该插件中定义的任何插件事件。 $dispatcher = JDispatcher::getInstance(); $data = array($argu1, $argu2); // any number of arguments you want return $dispatcher->trigger($eventName, $data); You can call any event of plugin which is defined in that plugin. $dispatche ...
  • 您不需要按照建议的方式抓取数据,尤其是当您需要的数据已经存在于本地时,绝不应该这样做。 您可以使用Joomla代码,如下所示: $article = JTable::getInstance('content'); $article->load($article_id); echo $article->get('fulltext'); 只需将$ article_id替换为文章的ID即可。 您可以使用所需的字段替换全文。 I found a useful tutorial on this. It's stil ...
  • IIRC,即使组件安装不正确,根据我的经验,您仍然可以访问服务器上的代码。 但这可能是1.5的事情。 如果您没有通过传递&view = viewname来指定URL中的视图,并且您没有设置默认的回退视图,那么您不能指望出现任何问题。 有关如何设置默认视图的一个很好的示例,请查看components/com_contact/controller.php第38行。 在主控制器的显示方法中: // Set the default view name and format from the Request. $vN ...
  • 在Joomla! 2.5我们定期创建“内容”只有只能访问“com_content”组件的后端用户。 回到我们的步骤,您唯一没有指定的是将新用户组添加到View Access Level 。 如果您在设置Joomla时使用的是标准样本内容! 您可以将新用户组添加到“特殊”视图访问的网站(尽管任何会这样做)。 如果一个新组具有“管理员登录”权限,它可以登录,但没有View Access级别,它看不到任何东西..这是一个微妙的区别,但它是Joomla的方式! 作品。 In Joomla! 2.5 we regul ...
  • 可能为时已晚,但这个话题只是我找到了那个麻烦。 可能是我的解决方案对某人有帮助。 1)添加.xml表单文件的标记属性'addfieldpath',如下所示:
    2)修改过滤后的字段描述如下:
  • 在尝试了`#__ menu`表后,我发现了许多有趣的功能。 在joomla安装数据库中有一个名为#__menu的数据库表。 每列的功能如下所述。 id(INT) 主键。 title(VARCHAR) 显示菜单的名称。 非常重要的领域。 菜单类型(VARCHAR) *告诉菜单是“主菜单”,“顶层菜单”,“左菜单”.... 管理员菜单应该具有“主要”值。 但是它们是否显示在后端是由'client_id'字段决定的(见下文),而不是这里。* 别名(VARCHAR) 'title'的别名。 但如果填充或不填充它不会 ...
  • Com_config管理该视图,它非常期望标准结构。 只要不破坏其他扩展,您就可以为它执行布局覆盖。 例如,您可以将该选项作为您的扩展条件。 Com_config manages that view and it pretty much expects a standard structure. You might be able to do a layout override for it as long as you don't break the other extensions. You could ...
  • 据我所知,您问题的最佳解决方案是在笔记本列表页面中提供“新建”和“编辑”按钮,并创建notbook的编辑视图。 与Joomla一样有文章和文章观点。 所以,如上所述,如果使用剂量没有任何笔记本,那么他/她可以使用“新”按钮创建一个新的笔记本。 当用户点击“新建”按钮时,他有空白表格,他/她可以创建新笔记本。 以下是如何在Joomla中创建列表和编辑视图的示例。 希望这会帮助你。 https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_b ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)