Solved

LINQ and recursive query

Posted on 2014-03-31
6
253 Views
Last Modified: 2014-04-16
Is there any means to do recursive queries in LINQ?

For example when distinct products are unusable because the consumer need a specific class definition but the product needs to only return only one row for each unique instance of a property.

///PCode-

Class Stock
{
Prop Department as String
Prop Store as String
Prop AnalysisCode as String
}

Lst = List Of {Stock}

Lst.Add(NEW (Stock(A, B, K1 ))
Lst.Add(NEW (Stock(A, B, K2 ))
Lst.Add(NEW (Stock(A, A, K1 ))
Lst.Add(NEW (Stock(B, B, K3 ))

Open in new window


The proper output to the question as a list would be K1, K2, K3 committing the second K1 as a duplicate.  Even thought all instances are different and distinct.  The result only returns only the fist instance of AnalysisCode.
0
Comment
Question by:Alyanto
  • 3
  • 3
6 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39968401
Hi,

Do you only need AnalysisCode as an output, or the full class? If you need the full class, is there any business rule that A,B,K1 will be returned instead of A,A,K1. Do you want just one of them (like the first)?

Giannis
0
 

Author Comment

by:Alyanto
ID: 39974320
Apologies for the delay,  The result should be the full class.  I realised early on that I could bracket the query and use Tolist.Distinct to get a single value.  I started to consider how I might have used a SQL Server CTE recursive query to do the same job; but I could not see how I could do that in LINQ.  I have a few places where having this ability would improve code and performance.
0
 

Author Comment

by:Alyanto
ID: 40003424
I've requested that this question be deleted for the following reason:

There has been no response in some time and the expert was unable to give an answer that solved the problem
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39993902
Hi,

I really apologize, but i have forgot to give you a possible solution. If you still care for one, this could be it:

Public Class Stock
	Public Department As string
	Public Store As string
	Public AnalysisCode As string
End Class

Sub Main
	Dim Lst = New List(Of Stock)
	Dim lStock = New Stock
	lStock.Department = "A"
	lStock.Store = "B"
	lStock.AnalysisCode = "K1"
	Lst.Add(lStock)

	lStock = New Stock
	lStock.Department = "A"
	lStock.Store = "B"
	lStock.AnalysisCode = "K2"
	Lst.Add(lStock)

	lStock = New Stock
	lStock.Department = "A"
	lStock.Store = "A"
	lStock.AnalysisCode = "K1"
	Lst.Add(lStock)

	lStock = New Stock
	lStock.Department = "B"
	lStock.Store = "B"
	lStock.AnalysisCode = "K3"
	Lst.Add(lStock)
	
	Lst.GroupBy(Function(x) x.AnalysisCode).Select(Function(x) x.FirstOrDefault).Dump
End Sub

Open in new window


Really sorry for Keeping you wait.

Giannis
0
 

Author Closing Comment

by:Alyanto
ID: 40003425
I think I asked for the question to be deleted before Giannis responded.  However when I revisited it there was the correct answer.

May thanks

Giannis
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 40003437
Well, actually, because you asked for a deletion i got a notification which reminded me that i had not given the answer. Sorry for keeping you wait.

Giannis
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now