여기에서 얻으려고하는 것에 대한 매우 간단한 코드가 있습니다.EntityFramework : FindBy (열 이름) 표현식 트리로 임의의 열 선택
기본적으로 여기에있는 아이디어는 간단한 FindBy(x => x.<the_column_name>, <the value>);
을 수행 할 수 있도록하는 것이므로 열 이름 만 변경하여 동일한 쿼리를 복사하여 붙여 넣을 필요가 없습니다. 지금은 LINQ to Entities does not recognize the method 'System.String Invoke(Spunky.Entities.User)' method, and this method cannot be translated into a store expression
말을 LINQ에서 예외가 계속 들어
가능한 일이 있습니까? 아니면 EF 6.1이 아직 없습니까?
public class UsersRepository
{
private Lazy<IDataContext> context;
public UsersRepository()
: this(() => new DataContext())
{
}
public UsersRepository(Func<IDataContext> context)
{
this.context = new Lazy<IDataContext>(context);
}
public IQueryable<User> Find(int id)
{
return FindBy(u => u.Id, id);
}
public IQueryable<User> Find(string username)
{
return FindBy(u => u.Username, username);
}
public IQueryable<User> FindBy<T>(Func<User, T> property, T value)
{
return context.Value
.Users
.Where(u => property.Invoke(u).Equals(value));
}
}
Hum, 좋은 아이디어이지만'Expression.Equal()'은'BinaryExpression'을 렌더링하고'Where' 메소드는'Func'또는'Expression >'을 필요로하지만 이것을 마음에, 아마도 여기에 할 일이있을 것입니다. –
Erick
당신 말이 맞아요. 람다 예측을 잊어 버렸어. 업데이트를 참조하십시오. – user3411327