Check a list has set of values [duplicate] - c#

This question already has answers here:
Determine if a sequence contains all elements of another sequence using Linq [duplicate]
(4 answers)
Closed 8 years ago.
I have two sting lists
List<string> list1=new List(){"1","2","3"};
List<string> list2=new List(){"1","2"};
What will be the easiest way to check if list1 contains the values in list2.

How about
list1.Except(list2).Any();

Try using
[listName].Except(SecondListName).Any();
This should work.

Related

Query a list and select top 10 values [duplicate]

This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
Closed 4 years ago.
I have a list of objects in which each object has a property called "Frequency" and I want to be able to pick the top 10 objects that have the highest frequencies.
I saw some solutions that are kind of similar to what I am looking to solve using LINQ so any help is appreciated.
You can order the list by descending Frequency and then take the first 10 like this:
var top10 = objectList.OrderByDescending(o => o.Frequency).Take(10);

Sorting files in a List<string> by the elements filename number [duplicate]

This question already has answers here:
How can I order a List<string>?
(5 answers)
Natural Sort Order in C#
(18 answers)
Closed 5 years ago.
I have the following files in a List<string>
backup-codes (7).txt
backup-codes.txt
backup-codes (1).txt
backup-codes (2).txt
backup-codes (3).txt
backup-codes (8).txt
backup-codes (6).txt
How can I sort them by the number (eg. (#) ) so that files with the highest value always appear first in the list?
You can do something like
lstFiles = lstFiles.OrderByDescending(x=> int.Parse(Regex.Replace(x,"[^0-9]+","0"))).ToList<string>();
This assumes that there are no other digits in filename except for sequencing.
Here is fiddler : https://dotnetfiddle.net/Nb38fJ

Running a Distinct on a record [duplicate]

This question already has answers here:
Distinct in Linq based on only one field of the table
(10 answers)
Closed 7 years ago.
In LINQ if the result that I have is the a list of whole records from the database and I want to do a Distinct on one column of this record, How do I do that?
You could try something as simple as:
var distincts = records.Select(x=>x.ColumnName).Distinct();

Query a list of objects that contains any string [duplicate]

This question already has answers here:
Check if a string contains an element from a list (of strings)
(12 answers)
Closed 8 years ago.
I'm this expression to search inside a list of objects by a specific property:
var result = myObject.Where(o => o.SearchString.Contains(searchValue));
It works good for a single value. The searchValue is a string passed by the user. The user can pass a single word or many words separeted by spaces. Is there any way to filter the objects that contains any of the passed words?
I could do this with a loop, searching a new word in previous results, but it doesn't seem very elegant.
myObject.Where(o => words.Any(o.SearchString.Contains))

Converting Array Type in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert string[] to int[] in one string of code using LINQ
I have an array of strings I want to convert it to array of int
is there any way to convert it directly without looping
I mean without use foreach, for , LINQ select statement, etc.
Any suggestion please.
Array.ConvertAll: http://msdn.microsoft.com/en-us/library/exc45z53.aspx

Categories