Solved

LINQ and recursive query

Posted on 2014-03-31
6
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 1

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
 
LVL 1

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 
LVL 1

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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