Adding a where / order by clause to IQueryable

I have a function to query a set of records from a database:

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 } ); } 

I want to add an additional reason, for example

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

However, it seems to me that I need to create a new request from the existing one for this to work. But it seems that I work before that without creating a new request in the code snippet before:

  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)); } 

Is there any code that somehow doesn't need to create a new query for the original IQueryable object? Is a temporary object created?

Edit

Strange, but to make it work, I have to do the following:

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

The following actions will not be performed:

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

There is no error message, so the where clause is apparently ignored (it also returns all data for which PointsEarned is not 50)

+6
source share
4 answers

Your entries are an IQueryable variable, so it’s enough, and you can add any number of sentences before retrieving data (for example, before ToList() ). It does not start, only the expression tree is created until you get the data.

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

he is equal

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

See LINQ and Delayed Execution

+14
source

I mean, you can write this sample:

 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(.....); } 
+3
source

You need to create a new object. IQueryable is immutable. Do not worry how you should do it. Thus, requests are formed within the country. All extension methods, such as Where, do not actually modify the object. They just return a new one.

The code you require should not work. The method does not even have a type.

+2
source

As others have pointed out, you do not need a new object. Your syntax for OrderByDescending incorrect, but you need to specify a key selector.

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

Source: https://habr.com/ru/post/892470/


All Articles