Linqを使用して、DBクエリから各行を、カスタム型をその値として持つ辞書に投影しようとしています。これを行うにはLINQ構文がわかりませんか?Linq to SQLを使用してカスタムタイプの辞書にどのように投影するのですか?
これは私の現在の試みです(これはコンパイルされませんが、私がしようとしていることを示すはずです)。私は「新しい選択...」の部分に問題があります。
public class MyClass
{
public Dictionary<int, CustomType> dict;
}
public class MyType{
string Name;
decimal Value;
}
var result = (from t in table
select new {
t.Id,
new MyType(t.Name,t.Value)
}).AsEnumerable().ToDictionary();
ANSWER:
おかげでジェイソン。私は、コンストラクタではなく、プロパティと自動初期化子を使用しました。
public class MyType {
public string Name {get;set;}
public decimal Value { get; set;}
}
Dictionary<int, CustomType> dict;
dict = (from t in table
select new {
id = av.Account.Id,
mt = new MyType { Name = t.Name, Value = t.Value }
}).ToDictionary(item => item.id, item => item.mt);