2014-10-13 2 views
1

하나의 dict에 3 linq의 결과를 추가하고 싶습니다.사전에 추가 쿼리 여러 Linq 쿼리 결과

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; 

위의 코드는 결과가 dict 인 결과를 제공합니다. linq 개의 검색어 결과가 모두 포함 된 사전을 추가하는 방법은 무엇입니까?

+0

정확히 무엇이 오류입니까? –

+0

사전에는 AddRange 메서드가 없습니다 .. 키가 이미 있는지 여부를 확인하여 값을 하나씩 추가해야합니다. –

+0

[여러 사전을 하나의 사전에 결합] 가능한 복제본 (http://stackoverflow.com/questions)/10559367/combine-multiple-dictionaries-a-single-dictionary) –

답변

2

이 시도 :

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; 

몇 일을하지만, 고려 :

이 던질 경우 동일 (대소 문자 구분) a.FieldName가 같은 하나 개 이상의 테이블 번 이상 나타납니다 주어진 a.Id에 대한 표.

기본 DB에 이러한 문제가 발생하지 않도록 제한이 있습니까? 데이터의 성격이 특정 에 대해 하나의 a.FieldName이 합법적으로 한 번 이상 나타날 수있는 경우가 아니라면 아마 그럴 것입니다. 후자의 경우에는 ILookup으로 매핑하는 것이 더 적절할 수 있습니다.

a.FieldName이 대소 문자를 구분하지 않으면 대/소문자를 구분하지 않는 문자열 비교자를 사용하여 사전을 만듭니다.

a.Id,a.FieldName의 고유성이 3 개 테이블의 집계에 적용되는 경우 단일 테이블로 교체하는 것이 더 적합할까요?

0

먼저 IEnumerable을 만든 다음 끝에 Dictionary로 변환 할 수 있습니다.

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; 
    } 

다음은

[TestMethod] 
    public void LinqDictionary() 
    { 
     var dict = getDictionary(); 
     foreach (var item in dict) 
     { 
      Console.WriteLine("FieldName {0}, LabelName {1} ", item.Key, item.Value); 
     } 

    } 

출력 같은 것을 실현하려 도움이 될 것이다, 당신의 모델은 다음과 같습니다 제공

FieldName Field1, LabelName Label1 
FieldName Field3, LabelName Label3 
FieldName Field5, LabelName Label5 
0

여러 쿼리에서 사전에 항목을 추가하려면 linq을 조금 더 사용하고 사전에 첫 번째 세트를 직접 추가 한 다음 tolist 및 foreach를 사용하여 후속 항목을 추가하십시오. 퍼포먼스는 의심 스럽지만,이 경우 (컨트롤을 추가 할 때) 문제가 될 가능성이없고 TBH인데, 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()));