C# 添加到多个Linq查询的字典查询结果
我想把3linq的结果加到一个dict中C# 添加到多个Linq查询的字典查询结果,c#,linq,dictionary,C#,Linq,Dictionary,我想把3linq的结果加到一个dict中 Dictionary<string, string> dict = new Dictionary<string, string>(); dict = ctx.tbl1.Where(a => a.Id == cid).ToDictionary(a => a.FieldName, a => a.LabelName); dict = ctx.tbl2.Where(a
Dictionary<string, string> dict = new Dictionary<string, string>();
dict = ctx.tbl1.Where(a => a.Id == cid).ToDictionary(a => a.FieldName, a => a.LabelName);
dict = ctx.tbl2.Where(a => a.Id == cid).ToDictionary(a => a.FieldName, a => a.LabelName);
dict = ctx.tbl3.Where(a => a.Id == cid).ToDictionary(a => a.FieldName, a => a.LabelName);
return dict;
Dictionary dict=new Dictionary();
dict=ctx.tbl1.Where(a=>a.Id==cid).ToDictionary(a=>a.FieldName,a=>a.LabelName);
dict=ctx.tbl2.Where(a=>a.Id==cid).ToDictionary(a=>a.FieldName,a=>a.LabelName);
dict=ctx.tbl3.Where(a=>a.Id==cid).ToDictionary(a=>a.FieldName,a=>a.LabelName);
返回命令;
上面的代码给出了最后的
dict
结果。如何使用每个linq
查询结果添加到字典?您可以首先创建IEnumerable,然后在最后将其转换为字典
如果您的型号如下所示
public class DummyModel
{
private const decimal MinValue = 0m;
public int Id { get; set; }
public string FieldName { get; set; }
public string LabelName { get; set; }
//[DecimalValidatorAttrubute(precision: 2, maxdigits: 2, minValue: decimal.MinValue, maxValue: 99.99m)]
//public decimal Decimal { get; set; }
}
下面的方法
private Dictionary<string, string> getDictionary()
{
var tbl1 = new List<DummyModel>() { new DummyModel() { Id = 1, FieldName = "Field1", LabelName = "Label1" }, new DummyModel() { FieldName = "Field2", LabelName = "Label2" } };
var tbl2 = new List<DummyModel>() { new DummyModel() { Id = 1, FieldName = "Field3", LabelName = "Label3" }, new DummyModel() { FieldName = "Field4", LabelName = "Label4" } };
var tbl3 = new List<DummyModel>() { new DummyModel() {Id = 1,FieldName = "Field5", LabelName = "Label5" }, new DummyModel() { FieldName = "Field6", LabelName = "Label6" } };
const int cid = 1;
Dictionary<string, string> dict = new Dictionary<string, string>();
dict = tbl1.Where(a => a.Id == cid)
.Concat(tbl2.Where(a => a.Id == cid))
.Concat(tbl3.Where(a => a.Id == cid))
.ToDictionary(a => a.FieldName, a => a.LabelName);
return dict;
}
输出
FieldName Field1, LabelName Label1
FieldName Field3, LabelName Label3
FieldName Field5, LabelName Label5
试试这个:
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (var a in ctx.tbl1.Where(a => a.Id == cid)) dict.Add(a.FieldName, a.LabelName);
foreach (var a in ctx.tbl2.Where(a => a.Id == cid)) dict.Add(a.FieldName, a.LabelName);
foreach (var a in ctx.tbl3.Where(a => a.Id == cid)) dict.Add(a.FieldName, a.LabelName);
return dict;
Dictionary dict=new Dictionary();
foreach(ctx.tbl1.Where(a=>a.Id==cid)中的变量a)dict.Add(a.FieldName,a.LabelName);
foreach(ctx.tbl2.Where(a=>a.Id==cid)中的变量a)dict.Add(a.FieldName,a.LabelName);
foreach(ctx.tbl3.Where(a=>a.Id==cid)中的变量a)dict.Add(a.FieldName,a.LabelName);
返回命令;
尽管有一些事情需要考虑:
如果相同的(区分大小写)a.FieldName
出现在多个表中,或者对于给定的a.Id
在同一个表中出现多次,则会引发此错误
底层数据库是否具有阻止这种情况发生的约束?如果可能应该,除非数据的性质是一个a.FieldName
可以合法地为给定a.Id
出现多次。在后一种情况下,将它们映射到ILookup
可能更合适
如果a.FieldName
不区分大小写,请使用不区分大小写的字符串比较器创建字典
如果
a.Id,a.FieldName的唯一性适用于3个表的集合,那么将它们替换为一个表是否更有意义?要从多个查询中将项添加到字典中,我使用了更多的linq,直接将第一个集添加到字典中,然后使用tolist和foreach添加后续项。我怀疑性能是否超群,但在这种情况下(添加控件)ts不太可能起作用,而且我怀疑ToDictionary的执行速度是否明显更快
Dictionary<string, SomeObject> controlsChanged;
controlsChanged = this.Controls.OfType<TextBox>().Select(c => c.Name).ToDictionary(k => k, v => new SomeObject());
this.Controls.OfType<ComboBox>().ToList().ForEach(c => controlsChanged.Add(c.Name, new SomeObject()));
字典控件更改;
controlsChanged=this.Controls.OfType().Select(c=>c.Name).ToDictionary(k=>k,v=>newsomeobject());
ForEach(c=>controlsChanged.Add(c.Name,newsomeobject());
错误到底是什么?字典不包含AddRange方法。。您需要通过检查键是否已存在来逐个添加值。可能存在的重复项
Dictionary<string, SomeObject> controlsChanged;
controlsChanged = this.Controls.OfType<TextBox>().Select(c => c.Name).ToDictionary(k => k, v => new SomeObject());
this.Controls.OfType<ComboBox>().ToList().ForEach(c => controlsChanged.Add(c.Name, new SomeObject()));