Solved

LINQ Returning Duplicate Results and return a row number.

Posted on 2010-11-17
13
992 Views
Last Modified: 2012-05-10
I am receiving duplicate records with this query... any ideas?
Second question here, I need to know the row number for each record, via a count. Thanks.

      var members = from f in dc.MemberFollowerCounts
                                join m in dc.Members
                                on f.MemberId equals m.MemberId into joinedMemberCount
                                from m in joinedMemberCount.DefaultIfEmpty()
                                orderby f.FollowerCount descending
                                select new
                                {
                                      name = m.Name,
                                      biography = m.Biography,
                                      followers = f.FollowerCount,
                                      profileImageURL = m.ProfileImageURL,
                                      TwitterUserName = m.TwitterUserName.Replace("@",""),
                                      status = m.Status
                                };

0
Comment
Question by:Rob Gaudet
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 25

Expert Comment

by:kevp75
ID: 34163878
It's probably because of your Join.  more than likely there are more than one record in dc.Members that match records in dc.MemberFollowerCounts

for the count, you could add a variable in your loop, and add one each time it iterates
0
 

Author Comment

by:Rob Gaudet
ID: 34164402
Member to MemberFollowerCount is a one to many relationship. Result set incorrectly contains a new row for each record in MemberFollowerCount. I am looking for a single row for each member, that row should also show the highest FollowerCount (int) for each member from the follower count table. I could do this with a sub query in TSQL, not sure how to do this in LINQ.

Member (table)
MemberId
MemberName

MemberFollowerCounts (table)
MemberId
FollowerCount
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34166167
It looks like you are doing a Left Join, and not checking to see if "m" is null.
0
 

Author Comment

by:Rob Gaudet
ID: 34183948
I tried a left and right join. To no avail, any LINQ suggestions?
0
 
LVL 25

Expert Comment

by:kevp75
ID: 34187932
can you give us some test data?  I will see if I can get something going
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34189262
I agree that it is difficult for you, without knowing your data, to spot any cartesian products, which usually explain duplicate data...
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Rob Gaudet
ID: 34310756
I have this issue resolved. Thanks.
0
 
LVL 25

Expert Comment

by:kevp75
ID: 34310875
what did you do to resolve it?
0
 

Author Comment

by:Rob Gaudet
ID: 34310898
I abandoned the initial effort to join the tables. The follower count is now a scalar record in the member table instead of updated in a followercount table.
0
 

Author Comment

by:Rob Gaudet
ID: 34310914
I did have second question here that is pending.

I need to know the rank of the returned row.

Just an off the cuff example, that demonstrates..

from a in stuff
let counter = 0
select new { count = counter++, a.Name };
0
 

Author Comment

by:Rob Gaudet
ID: 34311078
BTW, I solved this as well, I used the Row_Number() function with OVER in SQL and called a stored proc from LINQ instead of LINQ. I would like to know how to increment a row from LINQ though.
-- Region Parameters

DECLARE @p0 Int = 0

DECLARE @p1 Int = 0

DECLARE @p2 NVarChar(1000) = '@'

DECLARE @p3 NVarChar(1000) = ''

-- EndRegion

SELECT ROW_NUMBER() OVER (ORDER BY [Followers] DESC) AS 'RowNumber', [t0].[Name] AS [name], [t0].[Biography] AS [biography], 

    (CASE 

        WHEN [t0].[Followers] IS NULL THEN @p1

        ELSE [t0].[Followers]

     END) AS [followers], [t0].[ProfileImageURL] AS [profileImageURL], REPLACE([t0].[TwitterUserName], @p2, @p3) AS [TwitterUserName], [t0].[Status] AS [status]

FROM [Member] AS [t0]

WHERE ([t0].[UpdatedOn] IS NOT NULL) AND ([t0].[Followers] > @p0) AND (((NOT ([t0].[IsDeleted] = 1)) AND (NOT ([t0].[IsInactive] = 1))) OR (([t0].[IsDeleted] IS NULL) AND ([t0].[IsInactive] IS NULL)))

ORDER BY [t0].[Followers] DESC

Open in new window

0
 

Accepted Solution

by:
ee_auto earned 0 total points
ID: 35154569
Question PAQ'd and stored in the solution database.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

18 Experts available now in Live!

Get 1:1 Help Now