linq list string like - linq

I have a list:
List(Of String)
I want to filter it with LINQ.
I use things like this:
newList = (From l In myList Where l Like String.Format("%{0}%", value)).ToList
Value is something like a or no, etc.
I want something like a LIKE in SQL:
like "%a%"
So I that will result a list of string where there is a a in each string.
How should I do that?
dim list1 as list(of string)
list1.Add("aaa")
list1.Add("bbb")
list1.Add("ccc")
list1.Add("abc")
list1 = from l in list1 where ???? 'like "a" return list1 with 2 item : "aaa" and "abc"

List a = new List();
a = a.Where(item => item.contains("a")).ToList();

In C#:
var newList = originalList.Where(item => item.Contains("a")).ToList();
In VB.Net:
dim newList = originalList.Where(Function(item) item.Contains("a")).ToList

(From I in Mylist
Where I.smth Contains(String)
Select I);
For a list of String you can do that :
(From I in Mylist
Where I.smth Contains(String(0)) Or I.smth Contains(String(1))...
Select I);
If you don't know how much strings you'll get, you might also need dynamic queries. It's quite more difficult and I won't be able to explain it

Related

Merge two or more lists in a list?

I have two queries. I want to assign the list values ​​returned from these two queries to a single list, send it to the view and meet it in the view. My goal is to learn to work with lists in C#.
var list1 = c.ETs.Where(p => p.prop != "yes").ToList();
var list2 = c.ETs.Where(p => p.prop == "yes").ToList();
You can merge two lists into one with this:
var newList = List1
.Concat(List2)
.ToList();
Though you could drop the ToList and work directly with the IEnumerable which means you don't need to create a new object.
However, this doesn't even need to be two queries since the Where clauses of both are opposite so they include the entire table, you could do:
var list = c.ETs.ToList();
Or if you want to have two different clauses that aren't simply opposites:
var list = ct.ETs
.Where(p => p.prop == "yes" || p.prop == "no")
.ToList()` for example
This will combine the values of both lists in a single list (the final output will be stored in List1):
List1.AddRange(List2);
Hi Ahmet you can try something like this but note you will need Linq:
var list3 = List1.Concat(List2).ToList();
or if you have more than 2 lists:
var list3 = list1.Concat(list2)
.Concat(list3)
.ToList();
Another method:
var list3 = List1.AddRange(List2);
Both above will create a new list containing both lists' items.

LINQ: how to do "flat" collection instead of "nested" [duplicate]

I have a LINQ query which returns IEnumerable<List<int>> but i want to return only List<int> so i want to merge all my record in my IEnumerable<List<int>> to only one array.
Example :
IEnumerable<List<int>> iList = from number in
(from no in Method() select no) select number;
I want to take all my result IEnumerable<List<int>> to only one List<int>
Hence, from source arrays:
[1,2,3,4] and [5,6,7]
I want only one array
[1,2,3,4,5,6,7]
Thanks
Try SelectMany()
var result = iList.SelectMany( i => i );
With query syntax:
var values =
from inner in outer
from value in inner
select value;
iList.SelectMany(x => x).ToArray()
If you have a List<List<int>> k you can do
List<int> flatList= k.SelectMany( v => v).ToList();
Like this?
var iList = Method().SelectMany(n => n);

Search multiple values in one column

How to select record based on multiple values search in one column using linq query.
like product id is "product1", "product2","product3" n number of values we have
You can use the .Contains method to check whether a value is within a list.
var values = new List<string>() { "Prod1", "Prod2", "Prod3" };
var query = context.Set<Product>().Where(x => values.Contains(x.Name));
You could use something like (this is VB.Net, change to C# if necessary)
Dim result = products.Where(Function(p) p.ID = "product1" Or p.ID = "product2" Or p.ID = "product3", ...)
Alternatively, you could pull it all back to the client and use .Contains, like so:
Dim materializedProducts = products.ToList()
Dim result = materializedProducts.Where(Function(p) {"product1", "product2", "{product3}", ...}.Contains(p.ID))
Going further still, you could create an extension method (generic, if that floats your boat) called IsIn or similar that allows you to swap the order of the collection and the search value:
<Extension()>
Public Function IsIn(Of T)(ByVal searchValue As T,
ByVal searchSet As IEnumerable(Of T))
Return searchset.Contains(searchValue)
End Sub
Dim materializedProducts = products.ToList()
Dim result = materializedProducts.Where(Function(p) p.ID.IsIn({"product1", "product2", "{product3}", ...}))

looping through 2 lists to get the results

I have two lists:
myObject object1 = new myObject(id = 1, title = "object1"};
myObject object2 = new myObject(id = 2, title = "object2"};
myObject object3 = new myObject(id = 3, title = "object3"};
//List 1
List<myObject> myObjectList = new List<myObject>{object1, object2, object3};
//List 2
List<int> idList = new List<int>{2, 3,5};
Now I need to get output as follows:
If a id is present in both the lists, I need to print "A",
if a id is present in list1 only, then I need to print "B",
...and if the id is present in list2 only, I need to print "C"
Can I use linq to achieve this?
I would simply use the inbuilt functions of Except and Intersect
List1.Intersect(List2) = "A"
List1.Except(List2) = "B"
List2.Except(List1) = "C"
There are plenty of resources online about how you could go about doing this, as one example (I didn't look into it too much), check out this link - Linq - Except one list with items in another
Hope this does the trick...

LINQ: Entity string field contains any of an array of strings

I want to get a collection of Product entities where the product.Description property contains any of the words in a string array.
It would look something like this (result would be any product which had the word "mustard OR "pickles" OR "relish" in the Description text):
Dim products As List(Of ProductEntity) = New ProductRepository().AllProducts
Dim search As String() = {"mustard", "pickles", "relish"}
Dim result = From p In products _
Where p.Description.Contains(search) _
Select p
Return result.ToList
I already looked at this similar question but couldn't get it to work.
Since you want to see if search contains a word which is contained in the description of p you basically need to test for each value in search if it is contained in the description of p
result = from p in products
where search.Any(val => p.Description.Contains(val))
select p;
This is c# syntax for the lambda method since my vb is not that great
Dim result = From p in products _
Where search.Any(Function(s) p.Description.Contains(s))
Select p
You can use a simple LINQ query, if all you need is to check for substrings:
var q = words.Any(w => myText.Contains(w));
// returns true if myText == "This password1 is weak";
If you want to check for whole words, you can use a regular expression:
Matching against a regular expression that is the disjunction of all the words:
// you may need to call ToArray if you're not on .NET 4
var escapedWords = words.Select(w => #"\b" + Regex.Escape(w) + #"\b");
// the following line builds a regex similar to: (word1)|(word2)|(word3)
var pattern = new Regex("(" + string.Join(")|(", escapedWords) + ")");
var q = pattern.IsMatch(myText);
Splitting the string into words with a regular expression, and testing for membership on the words collection (this will get faster if you use make words into a HashSet instead of a List):
var pattern = new Regex(#"\W");
var q = pattern.Split(myText).Any(w => words.Contains(w));
In order to filter a collection of sentences according to this criterion all you have to do its put it into a function and call Where:
// Given:
// bool HasThoseWords(string sentence) { blah }
var q = sentences.Where(HasThoseWords);
Or put it in a lambda:
var q = sentences.Where(s => Regex.Split(myText, #"\W").Any(w => words.Contains(w)));
Ans From => How to check if any word in my List<string> contains in text by #R. Martinho Fernandes

Resources