I'm trying to remove all records in a List<T> matching a where condition. What I did find in linq is the RemoveAll() method but it seems to only work by removing properties matching a condition instead of the complete row in the list.
So I did try a remove all as suggested here in conjunction with a Where clause which causes an "argument null exception".
Question:
How can you RemoveAll list rows matching a condition using linq?
//Sort the list by the UpdatedTime time descending
//Remove all records in list that are older than today's date and status equal to BB. Then order the remaining records desc.
var cuttOff = DateTime.UtcNow.AddDays(-10);
List<Escalation> escHistorySorted = escHistory.RemoveAll.Where(x => x.UpdatedTime <= cuttOff && x.status == "BB").OrderByDescending(d => d.UpdatedTime).ToList();
It looks like you're trying to do too much at once.
Remove the records first (move the predicate from your where clause directly inside RemoveAll), and then sort them.
var cuttOff = DateTime.UtcNow.AddDays(-10);
escHistory.RemoveAll(x => x.UpdatedTime <= cuttOff && x.status == "BB");
List<Escalation> escHistorySorted = escHistory.OrderByDescending(d => d.UpdatedTime).ToList();
The return value of RemoveAll is an integer representing the number of records removed, so you can't simply order the result of calling that method.
Related
I'm using DataTables.Mvc library for use with jQuery DataTables.
One of the methods is GetSortedColumns() which returns an array containing configurations for each column to be sorted.
Of interest in this object are the Name and SortDirection properties. Name is also the database table field name. SortDirection is either asc or desc.
At first ThenBy and ThenByDescending were undefined symbols, so I created ordered as IOrderedQueryable. This resolves the symbols, but I don't see any effect of these. Neither OrderBy, OrderByDescending, ThenBy nor ThenByDescending have any effect on the order of records in filteredRecords.
In Controller:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult GetUserSelections([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
{
// Column Sort
var filteredRecords = db.AspNetSelectorInputs.Select(si => si);
var sortedColumns = requestModel.Columns.GetSortedColumns();
var count = 0;
foreach (var column in sortedColumns)
{
var ordered = filteredRecords as IOrderedQueryable<AspNetSelectorInput>;
filteredRecords =
column.SortDirection == DataTables.Mvc.Column.OrderDirection.Ascendant
? count == 0
? ordered.OrderBy(c => column.Name)
: ordered.ThenBy(c => column.Name)
: count == 0
? ordered.OrderByDescending(c => column.Name)
: ordered.ThenByDescending(c => column.Name);
count++;
}
filteredRecords = filteredRecords.Select(si => si).Skip(requestModel.Start).Take(requestModel.Length);
....
Can anyone see why this doesn't affect ordering of filteredRecords?
Is there a better way?
It is sorting, on exactly what you've asked it to. But the lambda expressions aren't doing what you think. For example, you're doing .OrderBy(c => column.Name), which is sorting using a literal value of the name of the column which has the same value for every item in the collection (notice how the thing it is sorting on is not affected by c), so it appears not to sort your collection. For example, you might as well be doing .OrderBy(c => "Hello").
You would need to do something like .OrderBy(c => c.YourChoiceOfPropertyName). Except you can't do that because (presumably) the name of the property is a string value in column.Name. So you'll need to use reflection within the lambda to get the value of that property using c as the instance. This will need fixing on all the lambdas. For example, inside the loop:
var propertyInfo=typeof(AspNetSelectorInput)
.GetProperty(column.Name);
And replacement lambda expressions:
c=>propertyInfo.GetValue(c)
P.S. the two instances of .Select(si => si) seem to be redundant, unless I am missing something.
I am trying to do a OrderByDescending on my LINQ query. Currently I am doing OrderByDescending on ProcedureDate field.
Business Scenario:
A user can come in the application and add ChangeProcedureDates in application upto 3 times. So in database there are additional fields called ChangeProcedureDate1, ChangeProcedureDate2 and ChangeProcedureDate3.
What I am trying to accomplish:
I want to modify my LINQ query so it can check to see if in cases where ChangeProcedureDate1, ChangeProcedureDate2 and ChangeProcedureDate3 are NULL then OrderBy ProedureDate since there were no changes. If ProcedureDate, ChangeProcedureDate1 is filled and ChangeProcedureDate2 and ChangeProcedureDate3 are NULL then we need to OrderBy ChangeProcedureDate1. And same for ChangeProcedureDate2 and ChangeProcedureDate3. Basically I need to find the latest date entry among multiple DateTimeFields and do orderby on that.
Below is my LINQ query.
var unsortedItems = context.ReservationRequests
.Where(s => ((s.ReservationRequestStatusId.HasValue) &&
(s.ReservationRequestStatusId.Value == ReservationRequestStatusId)) &&
(Roles.Any(y => s.SubmitterGroupName == y) ||
s.CreatedBy == currentUserGuid))
.OrderByDescending(x => x.ProcedureDate ?? DateTime.MinValue);
I am having hard time trying to understand how can I do this in LINQ?
Try this:
.OrderByDescending(x => x.ChangeProcedureDate3 ?? x.ChangeProcedureDate2 ?? x.ChangeProcedureDate1 ?? x.ProcedureDate);
Or, change orders of properties as you need.
We have the list A which contain the random indexes in it. I have another list B which contains the class objects in it. I want to parse list B with the indexes present in list A and find the objects which have name Amar in it using Linq.
For example:
List<int> Indexes = new List<int>(); // This contains the random indexes
List<Student> StuObj = new List<Student>();
Class Student
{
String name;
}
Now I want to parse the list StuObj with the respect to the indexes present in the list Indexes and get the Student object indexes present in the list StuObj where the name is Amar.
You can do that using Linq. The Where has an overload that provides the index of the element:
List<int> indexes = new List<int>() { 5, 1 , 10, 30 };
var results = listB.Where((item, index)=> indexes.Contains(index)
&& item.Name == "Amar")
.Select(x => listB.IndexOf(x)).ToList();
Edit: to get the index of the element in the original listB, you can make use of the IndexOf(T) method.
This should work:
var result = Indexes
.Select(i => StuObj[i])
.Where(s => s.name = "Amar").ToList()
It performs fast index lookup to fetch only required objects. If you know there is only one record, you can use First or FirstOrDefault instead of ToList.
Assuming you have two lists, List indexList and List dataList, you can use select as follows:
indexList.Select(i => dataList[i]);
You should consider what you wish to happen if indexList contains an integer < 0 or > the size of dataList. For example, you could replace invalid entries with null, like:
indexList.Select(i => i < 0 || i >= dataList.Count ? null : dataList[i]);
Or you could filter them out like:
indexList.Where(i => i>=0 && i < dataList.Count).Select(i => dataList[i]);
Or you may know via preconditions that you will never have items in the index list that are out of the range of valid values.
EDIT
Based on the updated question, try this:
dataList.Where((item, index) => indexList.Contains(index) && item.Name == "Amar")
.Select(item => dataList.IndexOf(item));
This takes advantage of the Select and Where overloads that take the index of the item. The Where clause selects the item where the item's index in dataList is in the indexList, and also where the item's Name is Amar. The Select clause then returns the index of the item in the original data list.
Something like this:
var result = listA
.Where(i => i >= 0 && i < listB.Count)
.Select(i => listB[i])
.FirstOrDefault(b => b.Name == "Amar");
Basically you use the value from listA as index of an element of the listB. If you are sure that listA contains only valid indexes, then Where call can be removed.
EDIT: As per updated question, the answer is even easier:
var result = listA
.Where(i => i >= 0 && i < listB.Count && listB[i].Name == "Amar")
.ToList();
I see absolutely no reason to use the linear searching (hence slow) methods Contains and IndexOf as suggested in some other answers.
I need do a filter that request data with a parameter included in a list.
if (filter.Sc.Count > 0)
socios.Where(s => filter.Sc.Contains(s.ScID));
I try on this way but this not work, I tried also...
socios.Where( s => filter.Sc.All(f => f == s.ScID));
How I can do a filter like this?
socios.Where(s => filter.Sc.Contains(s.ScID));
returns a filtered query. It does not modify the query. You are ignoring the returned value. You need something like:
socios = socios.Where(s => filter.Sc.Contains(s.ScID));
but depending on the type of socios the exact syntax may be different.
In addition to needing to use the return value of your LINQ .Where(), you have a potential logic error in your second statement. The equivalent logic for a .Contains() is checking if Any of the elements pass the match criteria. In your case, the second statement would be
var filteredSocios = socios.Where( s => filter.Sc.Any(f => f == s.ScID));
Of course if you can compare object-to-object directly, the .Contains() is still adequate as long as you remember to use the return value.
I want to query my item in table Items, where the last update of each item must be less than 91 days old (from last update till now) and the quantity > 0.
This is my code in the Model:
public IList<Item> GetAllProducts()
{
var ien_item = from i in this.DataContext.Items
orderby i.LastUpdated descending
select i;
return ien_item.ToList().Where(
s =>
HelperClasses.HelperClass.IsLastUpdate(s.LastUpdated.Value) == true
&&
(s => s.Quantity) > 0
)
.ToList();
}
Anyone can solve it? Thanks.
We don't really know what's not working here. EDIT: Merlyn spotted it; your lambda syntax is messed up. There's more to do here though.
However, I'd have thought you'd want this:
public IList<Item> GetAllProducts()
{
var lastUpdateLimit = DateTime.UtcNow.Date.AddDays(-91);
var query = from item in DataContext.Items
where item.Quantity > 0 && item.LastUpdated >= lastUpdateLimit
orderby item.LastUpdated descending
select item;
return query.ToList();
}
Note that this is able to do all the querying at the database side instead of fetching all the items and filtering at the client side. It does assume that HelperClasses.HelperClass.IsLastUpdate is simple though, and basically equivalent to the filter I've got above.
(One additional point to note is that by evaluating UtcNow.Date once, the result will be consistent for all items - whereas if your code evaluates "today" on every call to IsLastUpdate, some values in the query may end up being filtered against a different date to other values, due to time progressing while the query is evaluating.)
EDIT: If you really need to use HelperClasses.HelperClass.IsLastUpdate then I'd suggest:
public IList<Item> GetAllProducts()
{
var query = from item in DataContext.Items
where item.Quantity > 0
orderby item.LastUpdated descending
select item;
return query.AsEnumerable()
.Where(s => HelperClass.IsLastUpdate(s.LastUpdated.Value))
.ToList();
}
... then at least the quantity filter is performed at the database side, and you're not creating a complete buffered list before you need to (note the single call to ToList).
The problem is your lambda syntax. You're trying to define a second lambda while in the middle of defining a first lambda. While this is possible to do, and useful in some contexts, it is sort of an advanced scenario, and probably won't be useful to you until you know you need it.
Right now, you don't need it. Unless you know you need it, you don't need it :)
So -
Instead of what you've written:
.Where(
s =>
HelperClasses.HelperClass.IsLastUpdate(s.LastUpdated.Value) == true
&& (s => s.Quantity) > 0
)
Write this instead:
.Where(
s =>
HelperClasses.HelperClass.IsLastUpdate(s.LastUpdated.Value) == true
&& s.Quantity > 0 // Notice I got rid of the extra lambda here
)
If you're morbidly curious:
The compile error you got is because you didn't define your second lambda correctly. It redefined a variable you'd already used (s), and you were trying to check if a lambda was greater than zero. That makes no sense. You can only compare the result of a lambda to some value. It's like calling a function. You don't compare functions to numbers - you compare the result you get when calling a function to a number.
Easy ...
public IList<Item> GetAllProducts()
{
var ien_item =
from i in DataContext.Items
where
HelperClasses.HelperClass.IsLastUpdate(i.LastUpdated.Value)
&& s.Quantity > 0
orderby i.LastUpdated descending
select i;
return ien_item.ToList();
}
Linq to SQL: Methods are not allowed (linq is not magic and can not convert C# methods to TSQL)
http://msdn.microsoft.com/en-us/library/bb425822.aspx
Linq to Object: while looking the same, it is much more powerful than linq to SQL... but can not query SQL databases :)
http://msdn.microsoft.com/en-us/library/bb397919.aspx
Linq to XML: same as linq to Object, with xml object
http://msdn.microsoft.com/en-us/library/bb387098.aspx
Linq to Dataset: not the same as Linq to SQL !
http://msdn.microsoft.com/en-us/library/bb386977.aspx
Other linq providers:
http://en.wikipedia.org/wiki/Language_Integrated_Query