我有一个字典列表,其中包含字符串类型和值为int的键.
许多词典中都有相同的键,但不是全部.
所以我的问题是:使用LINQ如何在所有字典中找到与每个不同键相关联的最大值?
例如,给出以下输入:
var data = new List<Dictionary<string, int>>
{
new Dictionary<string, int> {{"alpha", 4}, {"gorilla", 2}, {"gamma", 3}},
new Dictionary<string, int> {{"alpha", 1}, {"beta", 3}, {"gamma", 1}},
new Dictionary<string, int> {{"monkey", 2}, {"beta", 2}, {"gamma", 2}},
};
Run Code Online (Sandbox Code Playgroud)
我想要某种包含以下内容的集合:
{"alpha", 4},
{"gorilla", 2},
{"gamma", 3},
{"beta", 3},
{"monkey", 2}
Run Code Online (Sandbox Code Playgroud)
(我现在正在遍历列表并自己跟踪事物,真的只是想知道是否有更好的LINQ式方式)
编辑:我也不知道字符串键是什么提前
我第一次使用SqlServer,并且在我们的每个创建过程脚本中都有一个代码块,如下所示,如果已存在则删除该过程:
IF EXISTS (SELECT *
FROM information_schema.routines
WHERE routine_name = 'SomeProcedureName'
AND routine_type = 'PROCEDURE'
BEGIN
DROP PROCEDURE SomeProcedureName
END
//then the procedure definition
Run Code Online (Sandbox Code Playgroud)
要停止在每个文件中剪切和粘贴此样板代码,我希望将此代码放在其自己的存储过程中,以便脚本看起来像这样:
DropIfRequired('SomeProcedureName')
//then the procedure definition
Run Code Online (Sandbox Code Playgroud)
我尝试解决方案是:
CREATE PROCEDURE DropIfRequired
(
@procedureName varchar
)
AS
IF EXISTS (SELECT * FROM information_schema.routines
WHERE routine_name = @procedureName
AND routine_type = 'PROCEDURE')
BEGIN
DROP PROCEDURE @procedureName
END
Run Code Online (Sandbox Code Playgroud)
但是我得到以下错误:
消息102,级别15,状态1,过程DeleteProcedure,第10行'@procedureName'附近的语法不正确.
任何想法如何做我想要的?
我们有一些代码,给定属性名称使用反射来实现Comparer.
我希望存储一个委托/ Func来获取值,而不是每次我们需要获取值时支付反映价格.
鉴于这样的类:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试着写一个能为我创建委托的函数
Func<T, object> CreateGetFuncFor<T>(string propertyName)
{
PropertyInfo prop = typeof(T).GetProperty(propertyName);
return (Func<T, object>)Delegate.CreateDelegate(typeof(Func<T, object>),
null,
prop.GetGetMethod());
}
Run Code Online (Sandbox Code Playgroud)
以下代码适用于获取名称
var person = new Person { Name = "Dave", Age = 42 };
var funcitonToGetName = CreateGetFuncFor<Person>("Name");
Console.WriteLine(funcitonToGetName(person));
var functionToGetAge = CreateGetFuncFor<Person>("Age");
Run Code Online (Sandbox Code Playgroud)
但对于Age proerty,它会抛出一个带有"绑定到目标方法的错误"消息的ArgumentException
我错过了什么?还有另一种方法吗?
在工作中我们在服务器上使用svn,但我在本地使用git(通过git-svn)来利用未准备好共享的速度/分支/提交等.
这个工作正常,但是如果出现问题,我只能在我的计算机上连续几天进行更改,并且希望在我们的文件共享上有一个私人备份回购,这让我很不舒服.
我使用如下命令创建了本地仓库的副本:
git clone --mirror MyRepo z:/MyRepo.git
Run Code Online (Sandbox Code Playgroud)
并将此作为远程添加到我的本地用这个:
git remote add backup z:/MyRepo.git
Run Code Online (Sandbox Code Playgroud)
在一天结束时保持备份存储库与本地存储库同步的最佳方法是什么?
我认为它是以下之一,但不知道它们是否相同或者不是什么权衡.
在本地存储库中: git push --mirror backup
或者在备份库中: git remote update
或者确实如果有更好的方法?