How to give another function’s returned value in LINQ.
I have a IEnumerable of objects(Say ObjColl). From that object collection, I have to filter the objects based on the return value of another function(say GetObjPropertyValue). My final output should be Dictionary of object and its propertyValue. This dictionary shouldn’t have the objects for which propertyvalue is nothing.
GetObjPropertyValue function takes Object as input and return its property as string. But some times it returns nothing if that property not exist on Objects.
My query is something like,
Dictionary(obj, string)=ObjColl.Where(Function(p) not GetObjPropertyValue(p) is nothing)
but in above query I should write code to store the return value of GetObjPropertyValue(p) and add obj-p and returnValue combination to the dictionary. How to write that??
For Example See below code:
The last line below code is wrong. but please suggest me how to correct it. My intention is, I want output collection(say dictionary) of each fruitname and its color(returned by other function call) for all the fruits which the GetFruitColor is not nothing(it can be empty or valid string).
[code] Private function GetFruitColor(fruit) as string
‘It returns fruit color.
‘If valid fruit and color not available, it returns “”(empty string)
‘If fruit is not valid Fruit, it returns nothing. End function
private sub MyMethod()
Create a list of strings.
Dim fruits As New List(Of String)(New String() _
{"apple", "passionfruit", "banana", "mango", _
"orange", "blueberry", "grape", "strawberry"})
Dim query = _fruits.Where(Function(fruit) k= GetFruitColor(fruit) if not k is nothing select fruit, k)
End Sub
In above code last line is wrong. So how to put getFruitColor in k, how to correct it to save the k value and give me output as dictionary of fruit and k value.
I would suggest:
In C# this would be:
Adjust accordingly for VB 🙂