1

私は2つのテーブルを持っています。外部キーを使用してデバイス テーブルにリンクするアセット テーブル。

アセットの device_name フィールドを取得するには、linq で次を使用します。

dim device as string = result.device.device_name

Result オブジェクトには、以前の linq to entity フレームワーク コマンドからの一意のレコードが含まれています。

ここで、エンド ユーザーがレポートに必要なフィールドを指定できるようにする必要があります。フィールドの文字列名になってしまうので、リフレクションを使ってみました

dim name as string = result.GetType().GetProperty("asset_name").GetValue(result)

これはasset_nameフィールドを返すので、関連する外部テーブルフィールド値を取得できるはずです。

dim device = result.GetType().GetProperty("device").GetValue(result).GetType.GetProperty("device_name").GetValue(result.device)

これは機能しますが、オブジェクトを指定する必要がありました。最初のテーブルにリンクする他のテーブルがあるため、追加のコードを記述して、どのオブジェクトをチェックし、手動で指定する必要がありますか? それとも私はこれに間違って入っていますか?ヘルプとアドバイスをいただければ幸いです。

4

1 に答える 1

0

よりクリーンな解決策が見つからなかったので、これが私がやった方法です:

dim field as string ="device.device_name" 
dim output as string =""
dim subtable as string ="" 

If field.Contains(".") Then 
    subtable = field.Substring(0, field.IndexOf(".")) 
    field = field.Substring(field.IndexOf(".") + 1)
End If
If subtable <> "" Then
  Select case subtable
    Case "device" 
        If Not IsNothing(result.device.GetType().GetProperty(field)) Then
            output = result.device.GetType().GetProperty(field).GetValue(result.device)
        End If
  End Select
Else
    If Not IsNothing(result.GetType().GetProperty(field)) Then 
        output= result.GetType().GetProperty(field).GetValue(reult)
    End If
End If
Return output
于 2013-11-18T19:55:26.417 に答える