score:0
you need to take a look at enumerable methods
for filtering use the where
list.where(x=>x.createddate < yourdate).tolist();
for ordering
list.orderby(x=>x.createddate).tolist();
score:0
imho the third way is ok.
but if you don't want filter you can implement a pagination when you retrieve your list. because if you put a large date range you don't resolve your performance issue.
score:1
all the linq approaches are great, but they iterate the list 3 times. if there are really lot of items, then maybe an old-fashioned way will be more efficient (that is, if you want all three scenarios at once, otherwise the linq answers are the way to go):
list<item> before = new list<item>();
list<item> after = new list<item>();
list<item> between = new list<item>();
foreach (var item in items)
{
if (item.createddate <= timebefore)
{
before.add(item);
}
else if (item.createddate >= timeafter)
{
after.add(item);
}
else
{
between.add(item);
}
}
score:1
you could use linq where:
static void main(string[] args)
{
item item1 = new item() { createddate = new datetime(2010, 11, 10), id = "1", name = "foo1", price = "10.00" };
item item2 = new item() { createddate = new datetime(2010, 11, 11), id = "2", name = "foo2", price = "11.00" };
item item3 = new item() { createddate = new datetime(2010, 11, 12), id = "3", name = "foo3", price = "12.00" };
item item4 = new item() { createddate = new datetime(2010, 11, 13), id = "4", name = "foo4", price = "13.00" };
item item5 = new item() { createddate = new datetime(2010, 11, 14), id = "5", name = "foo5", price = "14.00" };
item item6 = new item() { createddate = new datetime(2010, 11, 15), id = "6", name = "foo6", price = "15.00" };
item item7 = new item() { createddate = new datetime(2010, 11, 16), id = "7", name = "foo7", price = "16.00" };
item item8 = new item() { createddate = new datetime(2010, 11, 17), id = "8", name = "foo8", price = "17.00" };
list<item> items = new list<item>();
items.add(item1);
items.add(item2);
items.add(item3);
items.add(item4);
items.add(item5);
items.add(item6);
items.add(item7);
items.add(item8);
list<item> filtered = itemsbeforedate(items, new datetime(2010, 11, 16));
foreach (item i in filtered)
{
console.write(i.name);
}
console.read();
}
public static list<item> itemsbeforedate(list<item> items, datetime beforedate)
{
return items.where(i => i.createddate < beforedate).tolist();
}
public static list<item> itemsafterdate(list<item> items, datetime afterdate)
{
return items.where(i => i.createddate > afterdate).tolist();
}
public static list<item> itemsbetweendates(list<item> items, datetime startdate, datetime enddate)
{
return items.where(i => i.createddate >= startdate && i.createddate <= enddate).tolist();
}
prints:
foo1 foo2 foo3 foo4 foo5 foo6
score:2
use linq:
var itemsbefore = items.where(i => i.createddate <= timebefore);
var itemsafter = items.where(i => i.createddate >= timeafter);
var itemsbetween = items.where(i => i.createddate >= timestart && i.createddate <= timeend);
for ordering
var ordrered = items.orderby(i => i.createddate);
score:2
considering you have a list<>
, i suggest:
list<item> itemsbefore = items.findall(i => i.createddate <= timebefore);
list<item> itemsafter = items.findall(i => i.createddate >= timeafter);
list<item> itemsbetween = items.findall(i => i.createddate >= timestart && i.createddate <= timeend);
there is a subtle difference between what i suggested and what the other have suggested.
the .where
method doesn't "cache" the returned list, so if you do:
var filtered = items.where(condition);
foreach (var item in filtered)
{
}
foreach (var item in filtered)
{
}
your whole list will be parsed twice to search for the items that make the condition true. to solve this "problem" (sometimes it could be a problem) you can add a .tolist()
after the .where()
the list<>.findall()
returns a new list<>
with only the selected items. so you can enumerate it how many times you want, because it has been "materialized".
score:9
you can use linq
:
var beforedatex = items
.where(i => i.createddate.date < datex); // remove the .date if you want to include the time
var afterdatex = items
.where(i => i.createddate.date > datex);
var betweendates = items
.where(i => i.createddate.date >= datex && i.createddate.date <= datey);
you can use a foreach
or methods like tolist
to execute the query and materialize the result.
foreach(item i in beforedatex)
console.writeline("name:{0} createddate:{1}", i.name, i.createdat);
Source: stackoverflow.com
Related Query
- Filter large list based on date time
- Filter list by date and time MVC/C#
- List filter based on date
- How to filter a list based on another list using Linq?
- Using LINQ to filter a list of items based on those items' presence in another list
- LINQ query to filter based on Substring of a list
- LINQ filter list based on property value and filter the properties as well
- use LINQ to remove items from a list based on a date
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- Select First, Last, Min, Max Value based on Time From a List of Objects
- c# LINQ: Filter List of Files based on File Size
- How to filter list that has date and retrieve only data within 7 days
- LINQ Filter results based on values between another list
- Filter a list based on value in an order?
- Make a Query Expression ignore the Time and filter only the Date
- How to efficiently filter objects out of an (initially) large list of objects
- Filter a list based on another list condition
- Filter out events order by time based on their value
- Search by zip code and filter list within specific radius?
- Remove older file(s) from list based on create date
- Linq query to order a list based on date and get distinct
- Filter List collection based on property of child list contains a name
- Filter a List<T> based on another list that each instance of T contains
- OrderBy list for only Date but not Time
- Filter a generic list based on another list
- Get a list of files based on creation date
- Filter record based on child list
- How to filter linq query based on all of the values of one property from another list
- Linq filter a list of objects based on another list
- Filter a list based on a multiple options
More Query from same tag
- How is the .net Entity Framework overkill versus LinqToSql?
- How to: linq query
- Speed Up Performance - LINQ to get items in one LIST, that are not in another List
- Do i really need use AsQueryable() on collection?
- MVC 4. and Entity Framework Table Join
- C# Dynamic Linq Where Clause
- Dynamic Queries with Expressions based on runtime
- linq to sql to get single value
- Entity Framework & Linq Orderby
- Is it possible to set NLS_SORT using Nhibernate configuration?
- Convert subquery in LINQ
- Lambda expression for outer joins
- How do I use Linq to group a DataTable by multiple rows/columns into a new datatable with duplicate rows concatenated?
- MySQL Linq using .Contains(variable)
- AddRange() and LINQ copying issue
- C# algo required for creating cluster
- Asynchronous LINQ
- Cross Join of List Object items in C# Linq
- EF Query with conditional include that uses Joins
- LINQ: An item with the same key has already been added
- Why do I need LINQ if I use NHibernate-like ORMs?
- MongoDB C# - LINQ Contains against a string array throws ArgumentException
- Dynamic "Not" by parameter in LINQ (Or any other code for that matter)
- LINQ Method Syntax - how to accomplish dynamic Linq statements
- Linq to XML Parsing WMS Capabilities Document
- Add Item to a IEnumerable populated using Linq (vb.net)
- Entity framework: Any reason why my related entity isn't "lazy-loaded "?
- Can I break my report into groups with LINQ?
- In C# matching all files in a directory using regex
- Grouping a datatable using Linq generates error: "Definition of method <method> is not accessible in this context"