2012-02-23 33 views
0

どのように私は別の命名法のテーブルのlinqを使用して一般的な更新メソッドを作成します。 PS私は2つのフィールドのみ(両方を浮く)を更新する必要がありますが、その名前が一つのテーブルから別のものに変わり、そしてテーブルのIDの名前もLinq一般的なメソッド

私は私のDataContextにこれらのテーブルがあると変化します。

Person {ID(key), Name, X(float), Y(float)} 
Vehicle {Code(key), Model, Latitude(float), Longitude(float)} 
Building {Name(key), Model, Latitude1(float), Longitude1(float)} 

* テーブルは架空ですが、そのまま私のシナリオを表します。私は標準的な命名法を達成するために、テーブルフィールドの名前を変更することはできません。 (私はデータベースを作っていない)。

私はLinqで汎用メソッドを作成する必要があります。このような何か:事前に

public void UdpateCoordinates(tableName, idOfTable, fieldLatitude, fieldLongitude) 
{ 
    //And make a Update here of the fields (fieldLatitude, fieldLongitude). 
    //Example person would be fields X and Y the ones to update. 
} 

おかげ

+4

たぶん、あなたは、特定の質問をするべきではなく、「私は感謝....する必要があります。」 – ken2k

+1

共通のインターフェイスを実装するためにテーブルを表すC#クラスを変更できますか? – dtb

+0

他のアプリケーションがすでにbdを使用しているため、クラスを変更することはできません。ありがとう。 – ramo2712

答えて

0

まず、あなたはあなたのクラスの車両は、建物のインターフェイスを実装してくださいインターフェイスに

public interface IPosition { 
    public int ID {get ;} 
    public Latitude {get; set;} 
    public Longitude {get; set;} 
} 

を必要としています。ここで

はUdpateCoordinates方法の可能バージョンです:

public void UdpateCoordinates<T>(IEnumerable<T> table, int ID, float latitude, float longitude) where T: IPosition { 
     var entity = table.Where(x.ID == ID).FirstOrDefault(); 
     if (entity != null){ 
      entity.Latitude = longitude ; 
      entity.Longitude = longitude ; 
     } 

     //Save the entity here 
    }