List
에는 여러 개의 조인 된 테이블의 데이터베이스 행을 나타내는 Dictionary<string, string>
이 들어 있습니다. 모든 종류의 LINQ 접근 방식을 시도했지만 LINQ가이 데이터 구조에 대한 특정 작업을 허용하지 않는 벽을 항상 쳤습니다. 나는 사전 목록을 모델로 변환
KeyValuePairs
List
의 내부
Dictionary
에서 명시 적으로 추출 할 수 있습니다 그리고 그것은
Model
에 넣어 ... 그래서 핵심 문제를 잘못 설명없이 나는 내가 할 수있는 생각만큼 코드 예제를 단순화하기 위해 노력했다?
Model
이 subData와 동일한 작업을 수행 할 수있는 확장 가능한 솔루션이 필요합니다.
public class Program
{
public static void Main(string[] args)
{
List<Dictionary<string, string>> data = GetData();
data.Add(new Dictionary<string, string>() { ["modelData"] = "Bob", ["subModelData"] = "Frank" });
data.Add(new Dictionary<string, string>() { ["modelData"] = "Nancy", ["subModelData"] = "Boy" });
List<Model> models = new List<Model>();
//Fails in this linq statement. Anonymous types don't allow key accessors
foreach (Dictionary<string, string> distinctModel in data.GroupBy(x => new { x["dataKey"] }))
{
List<Dictionary<string, string>> newModelData = data.Where(d => d["data1Key"] == distinctModel["dataKey"]).ToList();
Model newModel = new Model(newModelData);
models.Add(newModel);
}
}
}
public class Model
{
public string modelData;
public List<Dictionary<string, string>> subData;
public Model(List<Dictionary<string, string>> data) {
modelData = data[0]["dataKey"];
data.Remove("dataKey");
subData = data;
}
}
'.GroupBy (x => new {x.GetValue ("dataKey")}) –
[TryGetValue()] (https://msdn.microsoft.com/en-us/library/bb347013(v=vs.110) .aspx)를 말하고 있습니까? 아직 간단한 이름이나 회원 액세스가 아닙니다. – Thor
@Thor - 샘플 데이터를 보여 주시겠습니까? 당신이 달성하고자하는 것을보다 명확하게 할 것입니다. –