正如标题所示,我正在寻找一种方法来将where子句与include结合使用.
这是我的情况:我负责支持一个充满代码味道的大型应用程序.更改过多代码会导致各处出现错误,因此我正在寻找最安全的解决方案.
假设我有一个对象总线和一个对象People(Bus有一个导航道具Collection of People).在我的查询中,我需要选择只有醒着的乘客的所有公共汽车.这是一个简单的虚拟示例
在当前的代码中:
var busses = Context.Busses.Where(b=>b.IsDriving == true);
foreach(var bus in busses)
{
var passengers = Context.People.Where(p=>p.BusId == bus.Id && p.Awake == true);
foreach(var person in passengers)
{
bus.Passengers.Add(person);
}
}
Run Code Online (Sandbox Code Playgroud)
在此代码之后,处理Context,并且在调用方法中,生成的Bus实体被映射到DTO类(实体的100%副本).
此代码导致多次调用DB,这是一个禁止,所以我在MSDN博客上找到了这个解决方案
这在调试结果时效果很好,但是当实体映射到DTO时(使用AutoMapper),我得到一个异常,即Context/Connection已经关闭并且无法加载该对象.(上下文总是关闭不能改变这个:()
所以我需要确保已经加载了Selected Passengers(导航属性上的IsLoaded也是False).如果我检查Passengers集合,Count也会抛出Exception,但是在Passegers集合中还有一个名为"包装相关实体"的集合,其中包含我的过滤对象.
有没有办法将这些包装的相关实体加载到整个集合中?(我无法更改automapper mapping配置,因为它在整个应用程序中使用).
有没有其他方式来获得活跃的乘客?
任何提示都是受欢迎的......
Gert Arnold的答案不起作用,因为数据未被急切加载.但是当我简化它并删除它的加载位置时.这真是奇怪,因为执行sql在两种情况下都返回所有乘客.因此,将结果放回实体时一定存在问题.
Context.Configuration.LazyLoadingEnabled = false;
var buses = Context.Busses.Where(b => b.IsDriving)
.Select(b => new
{
b,
Passengers = b.Passengers
})
.ToList()
.Select(x => x.b)
.ToList();
Run Code Online (Sandbox Code Playgroud)
经过很多努力,格特阿诺德的答案奏效了!正如Gert Arnold建议您需要禁用延迟加载并将其保持关闭状态.这将要求对应用程序进行一些额外的更改,因为上一代开发人员喜欢Lazy Loading -_-
我是一个C++新手,但我无法在网上找到这个(最有可能是微不足道的)问题的答案.我在编译两个类相互包含的代码时遇到了一些麻烦.首先,我的#include语句应该在我的宏内部还是外部?实际上,这似乎并不重要.但是,在这种特殊情况下,我遇到了麻烦.将#include语句放在宏之外会导致编译器递归并给我"#include嵌套太深"的错误.这似乎对我有意义,因为在调用#include之前,这两个类都没有完全定义.然而,奇怪的是,当我尝试将它们放入其中时,我无法声明其中一个类的类型,因为它无法识别.从本质上讲,这是我正在尝试编译的内容:
啊
#ifndef A_H_
#define A_H_
#include "B.h"
class A
{
private:
B b;
public:
A() : b(*this) {}
};
#endif /*A_H_*/
Run Code Online (Sandbox Code Playgroud)
BH
#ifndef B_H_
#define B_H_
#include "A.h"
class B
{
private:
A& a;
public:
B(A& a) : a(a) {}
};
#endif /*B_H_*/
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "A.h"
int main()
{
A a;
}
Run Code Online (Sandbox Code Playgroud)
如果它有所作为,我使用的是g ++ 4.3.2.
一般来说,#include语句应该去哪里?我一直看到它们超出了宏,但我清楚描述的场景似乎打破了这个原则.感谢任何帮助提前!如果我犯了任何愚蠢的错误,请允许我澄清我的意图!
我正在玩gmock并注意到它包含这一行:
#include <tuple>
Run Code Online (Sandbox Code Playgroud)
我原以为是tuple.h.
什么时候可以排除扩展名,它是否赋予指令不同的含义?
在调用之前如何检查include/require_once是否存在,我尝试将其放入错误块中,但PHP不喜欢它.
我认为file_exists()可以付出一些努力,但这需要整个文件路径,并且无法轻松地将相对包含传递给它.
还有其他方法吗?
根据运营商新的参考条目(http://www.cplusplus.com/reference/std/new/operator%20new/):
全局动态存储操作员功能在标准库中是特殊的:
- operator new的所有三个版本都在全局命名空间中声明,而不是在std命名空间中声明.
- 第一个和第二个版本在C++程序的每个翻译单元中隐式声明:不需要包含标题以使它们存在.
在我看来,这意味着在C++程序的每个翻译单元中都没有隐式声明第三版operator new(placement new),<new>并且需要包含标题才能使它存在.那是对的吗?
如果是这样,如何使用g ++和MS VC++ Express编译器,我似乎可以#include <new>在我的源代码中使用第三版new编译代码?
另外,关于operator new的MSDN标准C++库参考条目为包含该#include <new>语句的三种形式的operator new提供了一些示例代码,但是如果没有这个包含,该示例似乎对我来说编译和运行相同?
// new_op_new.cpp
// compile with: /EHsc
#include<new>
#include<iostream>
using namespace std;
class MyClass
{
public:
MyClass( )
{
cout << "Construction MyClass." << this << endl;
};
~MyClass( )
{
imember = 0; cout << "Destructing MyClass." << this << endl;
};
int imember;
};
int main( )
{
// The first form of new …Run Code Online (Sandbox Code Playgroud) /在一个链接的开头,到达根文件夹在php include中不起作用.
例如"/example/example.php"
解决办法是什么?
我试图在页面中包含一个php文件
require_once(http://localhost/web/a.php)
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误
Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0
Run Code Online (Sandbox Code Playgroud)
我改变allow_url_include=1了php.ini并且有效,但我不认为每个人都会让我更改他们的php.ini文件.
那么,有什么方法可以实现这一目标吗?
我正在使用constraintLyout v 1.0.1.
我想在我的xml中包含一个与我的全局布局的一部分相对应的子ConstraintLayout(它本身就是一个ConstraintLayout).我将布局分成两个xml,以便在其他地方使用此子部分
我尝试了这个但是我无法控制将子约束布局放在父级中的位置.我想知道是否必须将所有内容放在同一个xml文件中,或者它们是否是使用单独文件的解决方案.
tmp_1.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LABEL1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
/>
<TextView
android:id="@+id/label_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LABEL2"
app:layout_constraintStart_toStartOf="@id/label"
app:layout_constraintEnd_toEndOf="@id/label"
app:layout_constraintTop_toBottomOf="@id/label"
android:layout_marginTop="16dp"
/>
<include layout="@layout/tmp_2" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
tmp_2.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/view_80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="80th element"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="10dp"
android:layout_marginStart="12dp"
/>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
结果是此实际结果
但我希望它成为预期的结果
我试过这个,但它不起作用
<include
app:layout_constraintTop_toBottomOf="@id/label_2"
layout="@layout/tmp_2" />
Run Code Online (Sandbox Code Playgroud)
我很乐意有你的解决方案,
谢谢
我查看了C++编程语言,试图找到答案.当我#include "my_dir/my_header.hpp"在标题中,它在哪里寻找这个文件?是相对于包含它的源文件或其他内容的标题?
我正在寻找一个工具(最好是一个Visual Studio插件),它可以显示给定文件包含的所有文件,并显示这些文件包含的所有文件,依此类推.