Tag: C#的

LINQ:如何对集合中的所有对象的属性执行.Max(),并返回具有最大值的对象

我有一个有两个int属性的对象列表。 该列表是另一个linq查询的输出。 物体: public class DimensionPair { public int Height { get; set; } public int Width { get; set; } } 我想find并返回具有最大Height属性值的列表中的对象。 我可以设法获得Height值的最高值,但不是对象本身。 我可以用Linq做这个吗? 怎么样?

std :: enable_if有条件地编译一个成员函数

我试图得到一个简单的例子来了解如何使用std::enable_if 。 读完这个答案之后 ,我想应该不难想出一个简单的例子。 我想使用std::enable_if来select两个成员函数,并只允许使用其中的一个。 不幸的是,下面的代码不能用gcc 4.7进行编译,并且在几个小时和几个小时的尝试之后,我问你们我的错误是什么。 #include <utility> #include <iostream> template< class T > class Y { public: template < typename = typename std::enable_if< true >::type > T foo() { return 10; } template < typename = typename std::enable_if< false >::type > T foo() { return 10; } }; int main() { Y< double […]

entity framework – 包含多级属性

Include()方法在对象列表上工作得非常好。 但是如果我需要深入两层呢? 例如,下面的方法将返回ApplicationServers与这里显示的包含的属性。 但是,ApplicationsWithOverrideGroup是另一个容纳其他复杂对象的容器。 我也可以在该属性上执行Include()吗? 或者我怎么能得到该属性完全加载? 就目前而言,这种方法: public IEnumerable<ApplicationServer> GetAll() { return this.Database.ApplicationServers .Include(x => x.ApplicationsWithOverrideGroup) .Include(x => x.ApplicationWithGroupToForceInstallList) .Include(x => x.CustomVariableGroups) .ToList(); } 只填充Enabled属性(如下),而不填充Application或CustomVariableGroup属性(如下)。 我如何做到这一点? public class ApplicationWithOverrideVariableGroup : EntityBase { public bool Enabled { get; set; } public Application Application { get; set; } public CustomVariableGroup CustomVariableGroup { get; set; } }

如果在从开始到结束的迭代过程中调用地图元素上的erase()会发生什么?

在下面的代码中,我通过一个映射循环,testing一个元素是否需要被擦除。 清除元素并继续迭代是否安全?还是需要将密钥收集到另一个容器中,然后执行第二个循环来调用erase()? map<string, SerialdMsg::SerialFunction_t>::iterator pm_it; for (pm_it = port_map.begin(); pm_it != port_map.end(); pm_it++) { if (pm_it->second == delete_this_id) { port_map.erase(pm_it->first); } } 更新:当然,我然后读这个问题 ,我不认为会相关,但回答我的问题。

我如何使用reflection来调用私有方法?

在我的class级有一组私人的方法,我需要根据input值dynamic调用一个。 调用代码和目标方法都在同一个实例中。 代码如下所示: MethodInfo dynMethod = this.GetType().GetMethod("Draw_" + itemType); dynMethod.Invoke(this, new object[] { methodParams }); 在这种情况下, GetMethod()不会返回私有方法。 我需要提供什么BindingFlags到GetMethod()以便它可以find私有方法?

如何使用存储过程DbContext.Database.SqlQuery <TElement>(sql,params)? EF代码第一CTP5

我有一个存储过程有三个参数,我一直在尝试使用以下来返回结果: context.Database.SqlQuery<myEntityType>("mySpName", param1, param2, param3); 起初我尝试使用SqlParameter对象作为参数,但是这不起作用,并抛出一个SqlException与下面的消息: 过程或函数“mySpName”需要参数“@ param1”,它没有提供。 所以我的问题是如何使用这个方法与期望参数的存储过程? 谢谢。

编译器不明确的调用错误 – 具有Func <>或Action的匿名方法和方法组

我有一个场景,我想使用方法组语法而不是匿名方法(或lambda语法)来调用函数。 该函数有两个重载,一个接受一个Action ,另一个接受一个Func<string> 。 我可以愉快地使用匿名方法(或lambda语法)调用这两个重载,但是如果使用方法组语法,则会得到Ambiguous调用的编译器错误。 我可以通过显式转换为Action或Func<string> ,但不要认为这是必要的。 任何人都可以解释为什么明确的演员应该是必需的。 下面的代码示例。 class Program { static void Main(string[] args) { ClassWithSimpleMethods classWithSimpleMethods = new ClassWithSimpleMethods(); ClassWithDelegateMethods classWithDelegateMethods = new ClassWithDelegateMethods(); // These both compile (lambda syntax) classWithDelegateMethods.Method(() => classWithSimpleMethods.GetString()); classWithDelegateMethods.Method(() => classWithSimpleMethods.DoNothing()); // These also compile (method group with explicit cast) classWithDelegateMethods.Method((Func<string>)classWithSimpleMethods.GetString); classWithDelegateMethods.Method((Action)classWithSimpleMethods.DoNothing); // These both error with […]

将模板化的C ++类拆分成.hpp / .cpp文件 – 可以吗?

我遇到了错误,试图编译一个.hpp和.cpp文件之间拆分的C ++模板类: $ g++ -c -o main.o main.cpp $ g++ -c -o stack.o stack.cpp $ g++ -o main main.o stack.o main.o: In function `main': main.cpp:(.text+0xe): undefined reference to 'stack<int>::stack()' main.cpp:(.text+0x1c): undefined reference to 'stack<int>::~stack()' collect2: ld returned 1 exit status make: *** [program] Error 1 这是我的代码: stack.hpp : #ifndef _STACK_HPP #define _STACK_HPP template <typename Type> […]

C#获取%AppData%的path

C#2008 SP1 我正在使用下面的代码: dt.ReadXml("%AppData%\\DateLinks.xml"); 然而,我得到一个exception,指向我的应用程序正在运行的位置: 无法find部分path“D:\ Projects \ SubVersionProjects \ CatDialer \ bin \ Debug \%AppData%\ DateLinks.xml”。 我以为%AppData%应该find相对path。 当我Start|Run|%AppData% Windows资源pipe理器把我带到那个目录。 我不能把完整的path,因为每个客户机上的用户是不同的。 非常感谢您的任何build议,

在参数数量上重载macros

我有两个macrosFOO2和FOO3 : #define FOO2(x,y) … #define FOO3(x,y,z) … 我想要定义一个新的macrosFOO ,如下所示: #define FOO(x,y) FOO2(x,y) #define FOO(x,y,z) FOO3(x,y,z) 但是这不起作用,因为macros不会超过参数的数量。 在没有修改FOO2和FOO3 ,是否有某种方法来定义macrosFOO (使用__VA_ARGS__或其他方法)以获得将FOO(x,y)分配到FOO2和FOO(x,y,z)到FOO3的相同效果?