반응형
Linq 쿼리 결과를 사전으로 변환
Linq to SQL을 사용하여 데이터베이스에 일부 행을 추가하고 싶지만 행을 추가하기 전에 "맞춤 검사"를 수행하여 들어오는 행을 추가, 교체 또는 무시해야하는지 알고 싶습니다. 클라이언트와 DB 서버 간의 트래픽을 가능한 한 낮게 유지하고 쿼리 수를 최소화하고 싶습니다.
이 작업을 수행하기 위해 유효성 검사에 필요한만큼 정보를 가져 오지 않고 프로세스 시작시 한 번만 가져오고 싶습니다.
나는 이와 같은 일을 생각하고 있었지만 분명히 작동하지 않습니다. 누구나 아이디어가 있습니까?
Dictionary<int, DateTime> existingItems =
(from ObjType ot in TableObj
select (new KeyValuePair<int, DateTime>(ot.Key, ot.TimeStamp))
)
마지막에 가지고 싶은 것은 TableObject에서 전체 ObjectType 개체를 다운로드하지 않고도 Dictionary입니다.
나는 또한 다음 코드를 고려했지만 적절한 방법을 찾으려고 노력했다.
List<int> keys = (from ObjType ot in TableObj orderby ot.Key select ot.Key).ToList<int>();
List<DateTime> values = (from ObjType ot in TableObj orderby ot.Key select ot.Value).ToList<int>();
Dictionary<int, DateTime> existingItems = new Dictionary<int, DateTime>(keys.Count);
for (int i = 0; i < keys.Count; i++)
{
existingItems.Add(keys[i], values[i]);
}
사용해보십시오 방법 과 같이 :ToDictionary
var dict = TableObj.ToDictionary( t => t.Key, t => t.TimeStamp );
당신의 예를 보면, 이것이 당신이 원하는 것이라고 생각합니다.
var dict = TableObj.ToDictionary(t => t.Key, t=> t.TimeStamp);
다음을 시도하십시오
Dictionary<int, DateTime> existingItems =
(from ObjType ot in TableObj).ToDictionary(x => x.Key);
또는 본격적인 유추 버전
var existingItems = TableObj.ToDictionary(x => x.Key);
네임 스페이스 사용
using System.Collections.Specialized;
DataContext
Class 인스턴스 만들기
LinqToSqlDataContext dc = new LinqToSqlDataContext();
사용하다
OrderedDictionary dict = dc.TableName.ToDictionary(d => d.key, d => d.value);
값을 검색하려면 네임 스페이스를 사용하십시오.
using System.Collections;
ICollection keyCollections = dict.Keys;
ICOllection valueCollections = dict.Values;
String[] myKeys = new String[dict.Count];
String[] myValues = new String[dict.Count];
keyCollections.CopyTo(myKeys,0);
valueCollections.CopyTo(myValues,0);
for(int i=0; i<dict.Count; i++)
{
Console.WriteLine("Key: " + myKeys[i] + "Value: " + myValues[i]);
}
Console.ReadKey();
참고 URL : https://stackoverflow.com/questions/953919/convert-linq-query-result-to-dictionary
반응형
'Programing' 카테고리의 다른 글
파이썬의 람다에서 "if"를 수행하는 방법이 있습니까 (0) | 2020.03.08 |
---|---|
AngularJS에서 앵커 해시 링크를 처리하는 방법 (0) | 2020.03.08 |
URL에서 서버로 파일 다운로드 (0) | 2020.03.08 |
==와 strcmp를 사용한 문자열 비교 (0) | 2020.03.08 |
Git의 현재 지점 만 표시 (0) | 2020.03.08 |