Solved

LINQ Returning Duplicate Results and return a row number.

Posted on 2010-11-17
13
1,000 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
[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
  • 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

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
 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need return values from a stored procedure 8 44
MS SQL Conditional WHERE clause 3 38
ASP and Extracting XML 7 29
Need multiple Group By's 8 28
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 …

733 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