我可能正在使用错误的方法来发起查询。这是带有Sql Server后端的EF6。我有:
class Client(Model):
name = CharField(max_length=50)
email = EmailField(max_length=100, verbose_name="E-mail")
title = CharField(max_length=50)
department = CharField(max_length=50)
def get_string_fields(self):
# list of some excluded fields
excluded_fields = ['id', 'pk']
# getting all fields that available in `Client` model,
# but not in `excluded_fields`
field_names = [field.name for field in Client._meta.get_fields()
if field.name not in excluded_fields]
values = []
for field_name in field_names:
# get specific value from instanced object.
# and outputing as `string` value.
values.append('%s' % getattr(self, field_name))
# joining all string values.
return ' | '.join(values)
如果我用public IEnumerable<T> Select(string sessionId, Func<T, bool> predicate = null)
{
using (var databaseContext = new DatabaseContext())
{
var list = databaseContext.Set<T>().Where(predicate ?? (x => true)).AsQueryable<T>().ToList();
list.ForEach(o => o.SessionId = sessionId);
return list;
}
}
调用它,我希望它在sql中放置一个Select('sdfsdf', o => o.Category == 'Save Pewdiepie')
子句,但它给了我这个没有where
子句的内容:
where
对于大型表格来说,这将是一个大问题。如何更改它以创建SELECT
[Extent1].[Id] AS [Id],
[Extent1].[ClientId] AS [ClientId],
[Extent1].[Version] AS [Version],
[Extent1].[ReferenceNumber] AS [ReferenceNumber],
[Extent1].[RealExpirationDate] AS [RealExpirationDate],
[Extent1].[BusinessEffectiveDate] AS [BusinessEffectiveDate],
[Extent1].[BusinessExpirationDate] AS [BusinessExpirationDate],
[Extent1].[IsSuspended] AS [IsSuspended],
[Extent1].[RealEffectiveDate] AS [RealEffectiveDate],
[Extent1].[User] AS [User],
[Extent1].[Category] AS [Category]
FROM [dbo].[YouTube] AS [Extent1]
子句?
答案 0 :(得分:0)
我可以使用Expression
来修复它:
public IEnumerable<T> Select(string sessionId, Expression<Func<T, bool>> predicate = null)
{
using (var databaseContext = new DatabaseContext())
{
var list = databaseContext.Set<T>().Where(predicate ?? (x => true)).ToList();
list.ForEach(o => o.SessionId = sessionId);
return list;
}
}