, , , "" , , , . , :
public static class MyExtensions
{
public static int? GetId<TEntity>(this Context db, string name)
where TEntity : DomainEntity
{
return db.Set<TEntity>()
.Where(x => x.Name == name)
.Select(x => (int?)x.Id)
.FirstOrDefault();
}
}
, , . , , . nullable int , .
:
int? id = db.GetId<WindowStyle>("abc");
, WindowStyle .
, DomainEntity ( DbSet<DomainEntity>), . @Paul Keister .
Edit
:
public static class MyExtensions
{
public static int? GetId(this Context db, Type entityType, string name)
{
return ((IQueryable<DomainEntity>)db.Set(entityType))
.Where(x => x.Name == name)
.Select(x => (int?)x.Id)
.FirstOrDefault();
}
}
:
int? id = db.GetId("abc", someType);
, , someType DomainEntity. . , .