2015-05-19 10 views
6

ho bisogno di scrivere una funzione chiamata IsExists(string TableName,string KeyColumnName,string ValueToCheck) in DAL che verifica se esiste i dati nella tabella particolare e nella colonna particolare che sto passandodinamico Linq SearchFor da enti passando Nometabella, ColumnName e valore come parametro

fondamentalmente voglio ottenere qualcosa di simile quando provo a mettere in su nella query SQL

select count(id) from "+TableName+" where "+keyColumnName+"="+ValueToCheck+"; 

Ma io cant utilizzare query SQL ..

nella mia soluzione ho un file con estensione edmx, una classe di entità insieme a un repository classe, che ha SearchFor metodo:

public class EntityRepository<C, TEntity> : IEntityRepository<TEntity> 
     where TEntity : class 
     where C : DbContext 
{ 
    public IQueryable<TEntity> SearchFor(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) 
    { 
     return _entities.Set<TEntity>().Where(predicate); 
    } 
} 

ho provato qualcosa di simile

public bool CheckIsExists<T>(string keyColumnName, string valueToCheck) where T : class 
{ 
    bool isExist = false; 

    using (var repository = ContextFactory.CreateEmployeeMasterRepository()) 
    { 
     var repo = repository.GetEntityRepository<T>(); 
     object obj = (T)Activator.CreateInstance(type); 
     repo.SearchFor(u => u.GetType().GetProperty(keyColumnName).GetValue(obj).ToString() == valueToCheck); 
    } 
    return isExist; 
} 

Che non è di nuovo al lavoro ..

Qualcuno mi aiuti a fare questo. ho provato da molto tempo .. i suggerimenti sono molto apprezzati.

+0

Si dovrebbe controllare la mia domanda su LINQ dinamica: http://stackoverflow.com/questions/29234484/linq-dynamic-where-with-generic-property-and-value – W0lfw00ds

+0

1) Sembra come se hai un framework qui oltre a entityframework, ti ​​suggerisco di farlo funzionare prima con tipi/oggetti conosciuti prima di passare a generici. 2) Simula questo senza un database prima in un piccolo progetto di console – eugenekgn

risposta

0

È possibile eseguire query SQL da voi DbContext:

using (var ctx = new DBEntities()) 
{ 
    var itemsCount = ctx.Database.SqlQuery<int>("select count(id) from "+TableName+" where "+keyColumnName+" = "+ValueToCheck+").FirstOrDefault<int>(); 
} 
0

Ecco come vorrei risolvere questo tipo di problema. Potresti convertirlo in funzione se vuoi.

// Let's say I have a Customers table and want to search against its Email column 
// First I get my data layer class that inherits from DbContext class 
yourContextClass db = new yourContextClass(); 
// below I am getting valueToCheck from a view with GET method 
public ActionResult Index(string valueToCheck) 
{ 
bool isExists = false; 
IEnumerable<Customers> customersList = (from cus in db.Customers select cus).ToList(); 
int index = customersList.FindIndex(c => c.Email == valueToCheck); 
if (index >= 0) 
    isExists = True; 
// if index is -1 then the value to check does not exist 
return View(); 
} 
Problemi correlati