質問

もう一般的<>機能を取るlinqクエリー('項目')および列挙を通じて追加す。したいのですべて選択性の"ゆるっと"したアイテムは'ではなくちらの商品は一世帯(同一住所として以下のコードは)?

でに相当のsql:選択*,'bar'Fooとしてから項目

foreach (var item in items)
{
    var newItem = new {
        item, // I'd like just the properties here, not the 'item' object!
        Foo = "bar"
    };

    newItems.Add(newItem);
}
役に立ちましたか?

解決

あやのとうございますのだが、すべての種類のクライアントまで、フルのC#強い型であっても、匿名のもののようだ。しかしな不可能なワイヤーに引っ掛けて引っ張ってます。いように活用反射放自分で組み立てメモリを追加する新しいモジュールタイプが含まれる特定の物件です。ものを得ることができるプロパティのリストから匿名の項目:

foreach(PropertyInfo info in item.GetType().GetProperties())
    Console.WriteLine("{0} = {1}", info.Name, info.GetValue(item, null));

他のヒント

撮影書いたようでした。また一部のコード:/

その少し巻き込みが!:

ClientCollection coll = new ClientCollection();
var results = coll.Select(c =>
{
    Dictionary<string, object> objlist = new Dictionary<string, object>();
    foreach (PropertyInfo pi in c.GetType().GetProperties())
    {
        objlist.Add(pi.Name, pi.GetValue(c, null));
    }
    return new { someproperty = 1, propertyValues = objlist };
});
from item in items
where someConditionOnItem
select
{
     propertyOne,
     propertyTwo
};

いデザインはどのようなインテリアします。

反射です---しかし、すべての性質が知られるコンパイル時に、各項目のう方法をこのクエリを取得し何をする結果になっております。

こちらは一部の例法に署名:

public XElement ToXElement()
public IEnumerable ToPropertyEnumerable()
public Dictionary<string, object> ToNameValuePairs()

いと回収部のクラス:

   public int DepartmentId { get; set; }
   public string DepartmentName { get; set; }

その利用匿名タイプのようになります:

        List<DepartMent> depList = new List<DepartMent>();
        depList.Add(new DepartMent { DepartmentId = 1, DepartmentName = "Finance" });
        depList.Add(new DepartMent { DepartmentId = 2, DepartmentName = "HR" });
        depList.Add(new DepartMent { DepartmentId = 3, DepartmentName = "IT" });
        depList.Add(new DepartMent { DepartmentId = 4, DepartmentName = "Admin" });
        var result = from b in depList
                     select new {Id=b.DepartmentId,Damartment=b.DepartmentName,Foo="bar" };
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top