Solved

LINQ and recursive query

Posted on 2014-03-31
6
258 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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