2012-02-23 3 views
0

다른 명명법이있는 테이블에 대해 linq를 사용하여 일반적인 Update 메서드를 만드는 방법. PS 나는 단지 두 개의 필드 (두 수레) 업데이트해야하지만 그들의 이름은 한 테이블에서 다른 다양하고 테이블의 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 
    }