Расширение динамической библиотеки, безусловно, является вариантом, как уже предлагалось. альтернатива, учитывая, что Where
в Dynamic Linq возвращает Iqueryable
public static class DynamicQueryable {
public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values) { return (IQueryable<T>) Where((IQueryable) source, predicate, values); }
public static IQueryable Where(this IQueryable source, string predicate, params object[] values) {
используя DYnamic Object для контекста или «местоположений» репозитория.
Затем используйте where, который может содержать предикат динамической строки, и следуйте за ним с помощью firstOrDefault.
(захват или проверка на нуль не рассматриваются)
DynamicLocations.Where(x => x.IsPrimaryLocation).FirstOrDefault( ).Address1 as Address;
или динамический, где при необходимости
DynamicLocations.Where("IsPrimaryLocation",new string[]).FirstOrDefault( ).Address1 as Address;
Подробности. Вы можете предоставить доступ к методу общего класса репозитория, экземпляр которого вы создаете как динамический
public virtual IQueryable<TPoco> DynamicWhere(string predicate, params object[] values) {
return AllQ().Where(predicate, values);
}
Пример создания экземпляра динамического универсального репозитория
public class RepositoryFactory<TPoco> where TPoco : BaseObject,new() {
public IRepositoryBase<TPoco> GetRepository(DbContext context) {
// get the Pocotype for generic repository instantiation
var pocoTypes = new[] {typeof (TPoco)}; // but supports <T,U>
Type repBaseType = typeof (RepositoryBase<>);
IRepositoryBase<TPoco> repository = InstantiateRepository(context, repBaseType, pocoTypes);
return repository;
}
private IRepositoryBase<TPoco> InstantiateRepository(DbContext context, Type repType, params Type[] args) {
Type repGenericType = repType.MakeGenericType(args);
object repInstance = Activator.CreateInstance(repGenericType, context);
return (IRepositoryBase<TPoco>)repInstance;
}
}
person
phil soady
schedule
06.10.2014
FirstOrDefault()
может возвращать значениеnull
, которое вызовет исключение при попытке доступа кAddress1
(или любому другому) свойству. - person DavidG   schedule 06.10.2014FirstOrDefault
? - person Magnus   schedule 07.10.2014