Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,
I have one update to find string content is available in list within list collection,
How to solve this using LINQ, Any one give the better solution in C# or Vb.net

What I have tried:

MainList As List(Of List(Of String)) = Nothing

For Each subList As List(Of String) In MainList
        If subList(1)=Searchvalue Then
           retun true
        EndIf
     Next
Posted
Updated 19-May-17 21:34pm
v2
Comments
smksamy 20-May-17 3:01am    
Hi Anyone give Valuable Comment.

1 solution

Please, take a look at example:

VB.NET
Dim MainList As List(Of List(Of String)) = New List(Of List(Of String)) From _
	{ _
		New List(Of String) From {"a","b","c"}, _
		New List(Of String) From {"d","a","f", "c", "h"}, _
		New List(Of String) From {"i","c"}, _
		New List(Of String) From {"k","d","h"}, _
		New List(Of String) From {"e","o","p"}, _
		New List(Of String) From {"q","f","s", "g", "u"}, _
		New List(Of String) From {"v","z","g","z"} _
	}

Dim SearchValue As String = "a"

'#1
'returns sublists containing serached value
Dim subListContainingSerachedValue = _
	MainList _
	.Where(Function(x) x.Contains(SearchValue)) _
	.ToList()
	

'#2
'returns AnonymousType(Of String, String, Boolean)
Dim OnlySearchedValues = _
	MainList _
	.SelectMany(Function(x) _
			x.Select(Function(y) New With _
				{
					.OrigValue = y, _
					.SearchedValue = SearchValue, _
					.IsOnTheList = y.Any(Function(z) z=SearchValue) _
				}) _
		) _
	.ToList()


Results:

'#1
List<String> {"a", "b", "c"} 
List<String> {"d", "a", "f", "c", "h"} 


'#2
OrigValue SearchedValue IsOnTheList
a a True 
b a False 
c a False 
d a False 
a a True 
f a False 
c a False 
h a False 
i a False 
c a False 
k a False 
d a False 
h a False 
e a False 
o a False 
p a False 
q a False 
f a False 
s a False 
g a False 
u a False 
v a False 
z a False 
g a False 
z a False 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900