[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

LINQ Returning Duplicate Results and return a row number.

Posted on 2010-11-17
13
Medium Priority
?
1,015 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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