Entity Framework + Dynamic LINQ + Generic – Use on the Field which is existed on Multiple Table
Table Structure is applied on Following Code.
Table1_FKID / Table2_FKID – ( Pass Table Name ( Class Name ) & Column Name & Value on the Function Parameter )
SEQNUM – ( Return the Value from here ( plus 1 ) or Return 1 – if it is new item. )
Table 1 – Example
Table1_PKID | Table1_FKID | SEQNUM | … … |
---|---|---|---|
1 | 1 | 1 | … … |
2 | 1 | 2 | … … |
3 | 1 | 3 | … … |
4 | 2 | 1 | … … |
5 | 2 | 2 | … … |
6 | 3 | 1 | … … |
Table 2 – Example
Table2_PKID | Table2_FKID | SEQNUM | … … |
---|---|---|---|
1 | 1 | 1 | … … |
2 | 1 | 2 | … … |
3 | 1 | 3 | … … |
4 | 2 | 1 | … … |
5 | 3 | 1 | … … |
6 | 3 | 2 | … … |
public long getSeqNum<T>(long id, string field) where T : class { T obj = null; if ( typeof(T) == typeof( … … ) ) { // GetEntityList : DAO.Context.Set<T>().Where( …, … ); obj = GetEntityList<T>( field + " = @0", id ).Where( "… … == null" ).OrderBy( "SEQNUM descending" ).FirstOrDefault(); } else { // GetEntityList : DAO.Context.Set<T>().Where( …, … ); obj = GetEntityList<T>( field + " = @0", id ).OrderBy( "SEQNUM descending" ).FirstOrDefault(); } if ( obj != null ) { Type t = obj.GetType(); PropertyInfo prop = t.GetProperty("SEQNUM"); Object value = prop.GetValue(obj); if ( value != null ) return long.Parse(value.ToString()) + 1; else return 1; } else { return 1; } } |