Solved

LINQ and recursive query

Posted on 2014-03-31
6
256 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

22 Experts available now in Live!

Get 1:1 Help Now