Filter in collection c#
I have a collection in c# For ex:
Collection<User> List = null;
I need to fi开发者_如何学JAVAlter it based on sum parameters. so lets say i want to filter out users whos userID is between 1 to 100.
Please advice how i can do this in c#
Thanks Amit
Assuming you're using .NET 3.5 or higher, and you don't need the filtering to be in place, just use LINQ:
var filteredUsers = unfilteredUsers.Where(u => u.UserID < 1 || u.UserID > 100)
.ToList();
Note that this is filtering out users with IDs between 1 and 100 rather than filtering them in as per other answers.
If this doesn't help, please clarify the question. (Out of interest, why are you using Collection<T>
to start with?)
EDIT: If you really need a Collection<T>
you can create one easily enough:
var filteredUsers = new Collection<User>
(unfilteredUsers.Where(u => u.UserID < 1 || u.UserID > 100)
.ToList());
You could even add your own ToCollection
extension method to make this simpler. But Collection<T>
is usually meant to be the base class for more specific collection types (e.g. ObservableCollection<T>
) - it's odd to be constructing one directly. If your API is written in terms of Collection<T>
, you should potentially change it to be written in terms of IList<T>
, giving you more flexibility.
assuming the list is not null...
List.Where(u=>u.userId >=1 && u.userId <=100)
but this is so obvious i'm wondering if I understood the question correctly...
Linq has extensions for this, such as
var FilteredList = list.where(item => item >= 1 && item < 100);
List<users> Users = new myspeciallist() list.Addrange(Users.Where(z => userID < 101))
List<User> lst = null; //ATTENTION! you need to initialize a REAL collection here
List<User> myList = lst.FindAll(x=>x.userID>0 and x.userID<101);
You can also consider implementing a variation of the specification pattern. Advantages are decoupling from the User classes and easy changes if your business rule requirements change. It isn't hard to do.
A deliberately small example of the specification pattern (with lots of room for improvement such as a compound specification class that excepts enumerable specifications as constructor or single specifications, essentially combining them like Linq-to-XML does! Possibly make them static, read them from another assembly etc etc.)
public class User
{
public string name { get; set; }
public string userID {get;set; }
}
public interface ISpecification<T>
{
boolean IsSatisfiedBy(T obj);
}
public class IdValidUserSpecification : ISpecification<User>
{
public boolean IsSatisfiedBy(User user)
{
return user.userID >= 101 || user.userID < 1;
}
}
public class NameLongEnoughUserSpecification : ISpecification<User>
{
public boolean IsSatisfiedBy(User user)
{
return user.Name.Length > 10;
}
}
...
public void DoSomething()
{
ISpecification<User> idValid = new IdValidUserSpecification();
ISpecification<User> nameValid = new NameLongEnoughUserSpecification();
User user = new User();
user.name = "Jaapjan";
user.userID = 101;
if (nameValid.IsSatisfiedBy(user) && idValid.IsSatisfiedBy(user))
...;
}
var sds = from s in list where s.ProdId > 1 && s.ProdId < 100 select s
精彩评论