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))
Related
This question already has answers here:
How to get the first five character of a String
(22 answers)
Closed 2 years ago.
I have a string like this: first/exemple/import/status/5b12f918-f2ef-436f-808c-50ea48da000
I need to the output like this: first/exemple/import/
I need to remove from status and so on. I thought about remplace but it wont work because this 5b12f918-f2ef-436f-808c-50ea48da000 is variable so it changes.
if there any function can do this please
If you can guarantee the structure of the text (ie that structure won't change):
public static ReturnFirst(string inString){
var path = inString.Split('//')[1];
return $"//{path}//";
}
Or read the first 'x' characters of the string
This question already has answers here:
c# split string and remove empty string
(6 answers)
C# list count always returns 1 even when list is empty
(9 answers)
Closed 2 years ago.
I have a variable which stores some comma separated values as shown below
string Names ="Max,Amy,Anderson"
I have written a code which addds each of these comma seperated values to a List as shown below
List<string> listOfItens= new List<string>();
listOfItens = Names.Split(',').ToList();
Something I have noticed is that in a scenario where string Names =""; is empty the listOfItens will still have a count of 1 with the value "".
Any reason why it happens like that and is there a way I can get the listOfItens .Any() = null or listOfItens .count()==0
Came across this on SO but it didnt quite answer my question C# list count always returns 1 even when list is empty
This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 5 years ago.
I have an array of an object called storing, some of the object's attribute (skuID) is the same with some of the other storing with different (storingID), how do i make my array distinct depending on their (skuID)?
Storing(string storingID, skuID, storageID, price, expiry)
I have tried this but it doesn't work:
List<storing> storingAll = (List<storing>)Session["storingAll"];
List<storing> displayedStoring = storingAll.Distinct().ToArray();
storingAll.GroupBy(x=>x.skuID).Select(group=>group.First());
Note that this solution uses no external libraries. Simple group by the first occurrence of skuID.
Reference:
How to get a distinct list from a List of objects?
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.
This question already has answers here:
Get distinct items from a list
(4 answers)
Linq Distinct() by name for populate a dropdown list with name and value
(7 answers)
LINQ: Distinct values
(8 answers)
Closed 9 years ago.
I have a CarData object with the following properties:
PrimaryKey Make Model Year Drivetrain Country
I have about 1000 of these CarData objects in a List :
List<CarData> CarObjects
Is there a simple way to get a list of the distinct Makes?
var makes = CarObjects.Select(car => car.Make).Distinct();
This transforms the list from a list of CarData to a list of Makes, and then just finds the distinct values of the new list.
You can use Linq:
CarObjects.Select ( c => c.Make ).Distinct().ToList()
var makeList = CarObjects.Select(a => a.Make).Distinct();
Or
List<MakeEnum> = CarObjects.Select(a => a.Make).Distinct().ToList();
As an extra bit of advice, you may want to consider having Make be an enum, since there are (presumably) a finite (and rather small) number of possible makes of cars, instead of piling them into Strings. (You don't make a mention of what kind of property Make is, so maybe you are already doing this).