Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LINQ and recursive query

Posted on 2014-03-31
6
Medium Priority
?
266 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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