8 Replies - 2053 Views - Last Post: 05 April 2012 - 12:15 AM Rate Topic: **--- 2 Votes

#1 kothasiva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-April 12

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.

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




Is This A Good Question/Topic? 0
  • +

Replies To: IEnumerable.where in Linq how to get obj&return value of othr func

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1064
  • View blog
  • Posts: 4,167
  • Joined: 02-July 08

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

Was This Post Helpful? 0
  • +
  • -

#3 kothasiva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-April 12

Re: IEnumerable.where in Linq how to get obj&return value of othr func

Posted 04 April 2012 - 08:45 AM

View Post_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

Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1064
  • View blog
  • Posts: 4,167
  • Joined: 02-July 08

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?
Was This Post Helpful? 0
  • +
  • -

#5 kothasiva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-April 12

Re: IEnumerable.where in Linq how to get obj&return value of othr func

Posted 04 April 2012 - 09:02 AM

View Post_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.
Was This Post Helpful? 0
  • +
  • -

#6 kothasiva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-April 12

Re: IEnumerable.where in Linq how to get obj&return value of othr func

Posted 04 April 2012 - 09:08 AM

View Post_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.
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1064
  • View blog
  • Posts: 4,167
  • Joined: 02-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

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)
Was This Post Helpful? 0
  • +
  • -

#9 kothasiva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-April 12

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#
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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1