将where / order by子句添加到IQueryable

时间:2011-07-11 09:02:34

标签: c# linq

我有从数据库中查询一组记录的功能:

public IQueryable<PointTransactionViewModel> GetPointTransactions(int UserID)
        {
            return
                (
                    from PointTransaction p in entities.PointTransaction
                    join ActivityLog a in entities.ActivityLog
                    on p.TransactionID equals a.TransactionID
                    where p.UserID == UserID
                    select new PointTransactionViewModel
                    {
                        ID = p.TransactionID,
                        Balance = p.Balance,
                        Points = p.Amount,
                        RelatedActivityID = a.ID,
                        When = p.When,
                        Sender = p.SenderUserInfo.CompleteName
                    }
                );
        }

我想添加一个额外的原因,比如

var entries = GetPointTransaction(1);
return entries.OrderbyDescending.Where( x => x.When >= start && w.When <= end).
               ( x => x.When);

但是,我似乎需要从现有的查询中创建一个新查询才能生效。但是,在我之前的代码片段中,我似乎没有创建新查询,而是以前的工作:

 public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

上面的代码是否不需要为IQueryable源对象创建新的查询?是否创建了临时对象?

修改

这很奇怪,但为了让它发挥作用,我必须做到以下几点:

IQueryable<ActivityLogEntry> log = activityRepo.GetPointTransaction(userID).
   Where(x => x.PointsEarned == 50);
return log.ToList();

以下内容不起作用:

var log =  = activityRepo.GetPointTransaction(userID);
log.Where( x => x.PointsEarned == 50);
return log.ToList();

没有错误消息,只是忽略了where子句(它还返回了PointsEarned不是50的所有数据)

4 个答案:

答案 0 :(得分:14)

你的entries是一个IQueryable变量,所以它足够了,你可以在获取数据之前添加任意数量的子句(比如ToList()之前)。 它不会运行,只会创建表达式树,直到您获取数据。

var query = context.Where(x=>x.id == test);
query = query.Where(anotherCondition1);
query = query.Where(anotherCondition2);
...
var result = query.ToList();

等于

var result = context.Where(x=>x.id == test).Where(anotherCondition1)
                    .Where(anotherCondition2)....ToList()

请参阅LINQ and Deferred Execution

答案 1 :(得分:3)

我的意思是你可以写这个样本:

opportunites =  from opp in oppDC.Opportunities
join org in oppDC.Organizations on opp.OrganizationID equals org.OrgnizationID
select new
                        {
                            opp.OpportunityID,
                            opp.Title,
                            opp.PostedBy,
                            opp.Address1,
                            opp.CreatedDate,
                            org.OrganizationName
                        };
if(condition)
{
   opportunites  = opportunites.Where(opp => opp.Title.StartsWith(title));
}
//------Other Condition you need
if(!String.IsNullOrEmpty(title))
{
    opportunites  = opportunites.Where(.....);
}

if(!String.IsNullOrEmpty(name))
{
    opportunites  = opportunites.Where(.....);
} 

答案 2 :(得分:2)

您需要创建一个新对象。 IQueryable是不可改变的。不要担心这是你应该怎么做的。这就是内部形成查询的方式。像“Where”这样的所有扩展方法实际上都没有改变对象。他们只是换了一个新的。

您声明的代码不起作用。该方法甚至没有类型。

答案 3 :(得分:0)

正如其他人所指出的,你不需要新的对象。您的OrderByDescending语法错误,但您需要指定键选择器。

var entries = GetPointTransaction(1); 
return entries.Where(x => x.When >= start && w.When <= end).OrderbyDescending(x => x.When);