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]);
}
답변
답변
당신의 예를 보면, 이것이 당신이 원하는 것이라고 생각합니다.
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();
답변
