Hi,

I have an IEnumerable as a result of LINQ, which contains Value and Count. After I get the result, I want to do a string manipulation on the Value and make the corresponding Count to half. Is that possible with LINQ?

Thanks

Comments
Glad I wasted my time doing this.

Probably.

Post some code on what you are doing.

I'm sorry, I'm stuck again.

I have managed to get two IEnumerables with LINQ. They are like this.

A contains
Value: x Count: 4
Value: y Count: 2
Value: z Count: 5

B contains
Value: n Count: 3
Value: y Count: 1

I wish to join these values to get one List, so that the values look like this

Value: x Count: 4
Value: y Count: 3
Value: z Count: 5
Value: n Count: 3

How can I get this to work? Please help.

Thanks

Somebody please help me!!

This query would work if some experts give me a helping hand.

Dim q = From b in Item2 Join a in Item1 on b.Value Equals a.Value Select New With {b.Value, .Count = b.Count + a.Count}

This gives me a list of only that's common in both Item1 and Item2. I want the other items that were there in Item1 to still remain, add those in Item2 that are not there in Item1, and those common between Item1 and Item2 to have the Count value added.
Please help.

I cannot find another option :(

Thanks

Hopefully this will take care of it. Assuming this is what your talking of.

Dim L As New List(Of String)

        L.Add("Billy")
        L.Add("Shelly")
        L.Add("Billy")

        Dim item1 = From name In L _
                    Distinct _
                    Select Value = name, Count = Aggregate CheckName In L Into Count(CheckName = name)

        'item 1 now contains
        'Value = Billy, Count = 2
        'Value = Shelly, Count = 1

        Dim L2 As New List(Of String)
        L2.Add("Billy")
        L2.Add("Ronny")
        L2.Add("Shelly")
        L2.Add("Kelly")

        Dim item2 = From name In L2 _
                    Distinct _
                    Select Value = name, Count = Aggregate CheckName In L2 Into Count(CheckName = name)

        'item2 now contains
        'Value = Billy, Count = 1
        'Value = Ronny, Count = 1
        'Value = Shelly, Count = 1
        'Value = Kelly, Count = 1

        Dim q = From a In item1.Concat(item2) _
                Group By a.Value _
                Into Count = Sum(a.Count)

        'q now contains
        'Value = Billy, Count = 3
        'Value = Shelly, Count = 2
        'Value = Ronny, Count = 1
        'Value = Kelly, Count = 1

Or here is another example using Linq (with lambda expressions):

Class Program
	Private Shared Sub Main(args As String())
		Dim list As New List(Of [MyClass])()
		list.Add(New [MyClass]() With { _
			Key .Value = "x", _
			Key .Count = 2 _
		})
		list.Add(New [MyClass]() With { _
			Key .Value = "y", _
			Key .Count = 3 _
		})
		list.Add(New [MyClass]() With { _
			Key .Value = "n", _
			Key .Count = 1 _
		})
		list.Add(New [MyClass]() With { _
			Key .Value = "x", _
			Key .Count = 4 _
		})
		list.Add(New [MyClass]() With { _
			Key .Value = "y", _
			Key .Count = 1 _
		})

		Dim list2 = list.GroupBy(Function(g) g.Value).[Select](Function(s) New With { _
			Key .myValue = s.Key, _
			Key .MyCount = s.Sum(Function(x) x.Count) _
		}).ToList()
		's.Sum(t => t.Count)).ToList();
	End Sub
End Class

Class [MyClass]
	Public Property Value() As String
		Get
			Return m_Value
		End Get
		Set
			m_Value = Value
		End Set
	End Property
	Private m_Value As String
	Public Property Count() As Integer
		Get
			Return m_Count
		End Get
		Set
			m_Count = Value
		End Set
	End Property
	Private m_Count As Integer
End Class

If you want to create classes from the Linq query you can do:

Dim list2 = list.GroupBy(Function(g) g.Value).[Select](Function(s) New [MyClass]() With { _
	Key .Value = s.Key, _
	Key .Count = s.Sum(Function(x) x.Count) _
}).ToList()

Edited 5 Years Ago by Mitja Bonca: n/a

How about next time you respond when people give you solutions after you ask for them. If these solutions don't work I'll kiss your a__.

This article has been dead for over six months. Start a new discussion instead.