This question already has answers here:
How can I do an OrderBy with a dynamic string parameter?
(11 answers)
Closed 7 years ago.
Suppose I have this list of process or any other object
List<Process> listProcess = new List<Process>();
I can sort it using this line listProcess.OrderBy(p => p.Id);
But what if I have only string name of property obtained in runtime. I assume, I should use reflection to get the property object. Can I use orderby method or I should use Sort and then pass own comparer?
You can have a look at the post referred in the comment. Or, you can achieve that using simple reflection like this
var sortedList = list.OrderBy(o => o.GetType().GetProperty(propName).GetValue(o));
Where
List<object> list; //a list of any object(s)
string propName; //name of the property to be used in OrderBy
Related
This question already has answers here:
c# list.OrderBy not working at all? [duplicate]
(4 answers)
Closed 3 years ago.
I must order a list according to a string contained in each element of the list.
I have a ribbonDropDown element which contains a list of ribbonDropDownItem. Each of those item contains a string from which I want to reorder the item position in the ribbonDropDown alphabatically.
I think my problem can be solve just by considering I have a List of object which contain a string field.
I tried this :
List<myObject> myList = aList;
myList.OrderBy(i => i.Name, StringComparer.Ordinal);
and also
myList.OrderBy(i => i.Name);
I expect the result to be order from a->z or z->a depending on the method I use (OrderBy or OrderBy descening).
For now my result is the same order of my list before the operation.
Is it possible to use this method for that or should I use something else ?
I'd like not to use a for loop.
You need to reassign the return of the .OrderBy() to the original list.
myList = myList.OrderBy(i => i.Name).ToList();
.OrderBy() does not change the existing list, it returns the input list with the modifications as a new IEnumerable<>.
This question already has answers here:
In C# if an object in a list is added to another list, does changing the object in the second list change the same object in the first list?
(2 answers)
How to make correct clone of the List<MyObject>? [duplicate]
(3 answers)
Closed 5 years ago.
I have a List<dynamic> that I need to copy and then, based on a condition of the row of the list I need to modify a field of that row and add it to the second list.
This a sample of the code:
//list1 is a `List<dynamic>` that I get from a query using Dapper. I guess it is an ExpandoObject list
var list2 = new List<dynamic>(list1);
foreach (var obj in list2)
{
if (obj.condition == 1)
{
var newObj = obj;
newObj.description = "new row";
list2.Add(newObj);
}
}
My problem is that in both my list the obj in the list is updated with the string 'new row'.
It seems like every time I change newObj both lists are updated.
I also tried to create my list2 this way but I have the same problem:
var list2 = new BindingList<dynamic>(list1);
EDIT:
I looked at the other questions but in my case, I only have a dynamic List. Is it possible to get the result I want without having to create a Class and implement ICloneable?
Try
var list2 = list1.Select(x => x);
This question already has answers here:
List<T> OrderBy Alphabetical Order
(10 answers)
Closed 7 years ago.
I have a struct and a list as follow, I just wanted to sort the Inputpoints according to date. I have used the following commands but I can not see any sorting.
public struct Points
{
public Date Date;
public double Quantity;
}
_test = new List<Points>(InputPoints);
_test.OrderBy(t => t.Date);
Calling _test.OrderBy(t => t.Date) does not change the contents of _test itself, but rather returns a sorted IOrderedEnumerable<Points>. You can turn this back into a List<Points> using ToList(). So all in all
_test = _test.OrderBy(t => t.Date).ToList();
should do what you want.
From MSDN documentation:
"This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic."
Which means you have to use the value returned by OrderBy() in order to get sorted results.
_test =_test.OrderBy(t => t.Date).ToList();
This question already has an answer here:
Check for any element that exists in two collections
(1 answer)
Closed 9 years ago.
Is there a way to determine if a collection contains at least one element from another collection?
You can use the Any().
var listA = new List<int>();
var listB = new List<int>();
bool hasCommonItem = listA.Any(i => listB.Contains(i));
Moreover, you can write an IEqualityComparer implementation to pass it as a parameter to the Contains() if necessary.
Sure there is.
var sourceCollection = GetSourceCollection();
var otherCollection = GetAnotherCollection();
var hasAtLeastOne = sourceCollection.Intersect(sourceCollection).Any();
I assumed your collections are of the same type: IEnumerable<T> with the same T generic parameter.
It's gonna load whole sourceCollection first, and then fetch one element at a time from otherCollection until first common one is found.
col1.Any(x => col2.Any(y => x==y));
This question already has answers here:
How to convert IEnumerable to ObservableCollection?
(5 answers)
Closed 2 years ago.
I want to get all the places which are active for bid. I tries this but I'm getting null.
testObservableList = testObservableList.Where(
x => x.IsActiveForBid) as ObservableCollection<Places>;
try this.
testObservableList =
new ObservableCollection(testObservableList.Where(x => x.IsActiveForBid));
This will make a shallow copy of the current IEnumerable and turn it in to a ObservableCollection
While an ObservableCollection<T> is IEnumerable<T> the opposite does not hold. Try a constructor:
new ObservableCollection<SometypeType>(
testObservableList.Where(x => x.IsActiveForBid))
Try this:
ObservableCollection coll1 = new ObservableCollection(testObservableList.Where(x => x.IsActiveForBid);
I guess this will do it.