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"})
'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).
Dim query = _
fruits.Where(Function(fruit) k= GetFruitColor(fruit) if not k is nothing select fruit, k)
End Sub
8 Replies - 1109 Views - Last Post: 05 April 2012 - 12:15 AM
#1
IEnumerable.where in Linq how to get obj&return value of othr func
Posted 04 April 2012 - 07:46 AM
How to give another function's returned value in LINQ Ienumerable.
Replies To: IEnumerable.where in Linq how to get obj&return value of othr func
#2
Re: IEnumerable.where in Linq how to get obj&return value of othr func
Posted 04 April 2012 - 08:15 AM
Not sure how your function will return a color as not color property exists. If you use a custom class for Fruit you can define it's name, color and other things. Then define your List to hold that type:
Friend Class Fruit Friend Property Name As String Friend Color As Color End Class
Private fruits As New List(Of Fruit)'class level
Dim query = From f As Fruit In fruits Select f Where f.Color = <some color> If Not query Is Nothing Then 'query has results End If
#3
Re: IEnumerable.where in Linq how to get obj&return value of othr func
Posted 04 April 2012 - 08:45 AM
_HAWK_, on 04 April 2012 - 08:15 AM, said:
Not sure how your function will return a color as not color property exists. If you use a custom class for Fruit you can define it's name, color and other things. Then define your List to hold that type:
Friend Class Fruit Friend Property Name As String Friend Color As Color End Class
Private fruits As New List(Of Fruit)'class level
Dim query = From f As Fruit In fruits Select f Where f.Color = <some color> If Not query Is Nothing Then 'query has results End If
#4
Re: IEnumerable.where in Linq how to get obj&return value of othr func
Posted 04 April 2012 - 08:48 AM
So what does quoting my post mean? You understand, or not? What?
#5
Re: IEnumerable.where in Linq how to get obj&return value of othr func
Posted 04 April 2012 - 09:02 AM
_HAWK_, on 04 April 2012 - 08:15 AM, said:
Not sure how your function will return a color as not color property exists. If you use a custom class for Fruit you can define it's name, color and other things. Then define your List to hold that type:
Friend Class Fruit Friend Property Name As String Friend Color As Color End Class
Private fruits As New List(Of Fruit)'class level
Dim query = From f As Fruit In fruits Select f Where f.Color = <some color> If Not query Is Nothing Then 'query has results End If
Hawk,
MY actual need is as follows.
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 shouldnt 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 Obj.
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.
#6
Re: IEnumerable.where in Linq how to get obj&return value of othr func
Posted 04 April 2012 - 09:08 AM
_HAWK_, on 04 April 2012 - 08:48 AM, said:
So what does quoting my post mean? You understand, or not? What?
In Your example you are hardcoding output color value and returning fruits of that color. but my requirement is not that. I dont know which color I will get from the other function. I want the collection of fruit and color combinations as result.
#7
Re: IEnumerable.where in Linq how to get obj&return value of othr func
Posted 04 April 2012 - 09:46 AM
Funny you did not even mention, or code, a Dictionary in your first post. We don't guess what your needs are, you tell us up front. Show me how you create and fill the Dictionary.
#8
Re: IEnumerable.where in Linq how to get obj&return value of othr func
Posted 04 April 2012 - 01:59 PM
Enumerable always returns an object so, if the Sequence is empty it returns Enumerable.Empty(Of T)
#9
Re: IEnumerable.where in Linq how to get obj&return value of othr func
Posted 05 April 2012 - 12:15 AM
Got perfect solution:
Thanks to all who responded. The same question I have posted in other forum. I got perfect solution for this question from a person.
Following is the solution in C#
In VB.Net:
link: http://stackoverflow...-returned-value
Thanks to all who responded. The same question I have posted in other forum. I got perfect solution for this question from a person.
Following is the solution in C#
var query = fruits.Select(fruit => new { fruit, color = GetFruitColor(fruit) }).Where(pair => !string.IsNullOrEmpty(pair.color)).ToDictionary(pair => pair.fruit, pair => pair.color);
In VB.Net:
Dim query = fruits.[Select](Function(fruit) New With { _
fruit, _
Key .color = GetFruitColor(fruit) _
}).Where(Function(pair) Not String.IsNullOrEmpty(pair.color)).ToDictionary(Function(pair) pair.fruit, Function(pair) pair.color)
link: http://stackoverflow...-returned-value
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|