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));
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:
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
This question already has answers here:
Difference between Query Expression and Method Expression in LINQ?
(5 answers)
Closed 7 years ago.
I have a list of Pieces. I want an int[] of PieceID;
I try this, but this create an anonymous type[].
PiecesTop1 = new List<Piece>();
var test = PiecesTop1.Select(x => new {x.PieceID}).ToArray();
Then I found this syntaxis and works ok.
var lstPieceID = from p in PiecesTop1
select p.PieceID;
int[] arrPieceID = lstPieceID.ToArray();
int i = arrPieceID[0];
I usually use first sintaxis so not familiar with second one.
First question: How I make first sql to return an int[].
Second question: How can I call each of those sql query type to difference beetween them?
Is there any diference on what sintaxis should I use (just in case I'm not using any database).
var test = PiecesTop1.Select(x => x.PieceID).ToArray();
This question already has answers here:
Cast IList to List
(9 answers)
Closed 9 years ago.
I have a function that takes IList<string> someVariable as a parameter. I want to convert this to a list so I can sort the values alphabetically.
How do I achieve this?
you can just do
var list = new List<string>(myIList);
list.Sort();
or
var list = myIList as List<string>;
if (list != null) list.Sort; // ...
IList<string> someVariable = GetIList();
List<string> list = someVariable.OrderBy(x => x).ToList();
IList implements IEnumerable. Use the .ToList() method.
var newList = myIList.ToList();
newList.Sort();
You can use linq to sort an IList<string> like so:
IList<string> foo = .... ; // something
var result = foo.OrderBy(x => x);
You don't have to convert to a List to sort things. Does your sorting method require a List but not accept an IList. I would think this is the actual problem.
Furthermore if you really need a List, if your IList realy is a List (which is somewhat likely) you can just interpret it as such. So I would first check if it is already a List before creating a new one
var concreteList = parameter as List<T> ?? parameter.ToList();