MrDean
MrDean

Reputation: 3

'Random' Orderby in webservice using LINQ To Entities

Morning all

Now I know there is a reason to this odering but my tiny little brain can't get my head around it.

I am using a webservice to pull through data to a webp[age and have the following that is so far pulling data through from UUF1:

 public string[] GetBuyer(string Memberkey)
        {
            try
            {
                WebService.EntitiesConnection buyer = new WebService.EntitiesConnection();

                return buyer.tblProducts
                        .Where(p => p.MemberId == Memberkey)
                        .OrderBy(p => p.UnitUserfield1)
                        .Select(p => p.UnitUserfield1)
                        .Distinct()
                        .ToArray();
            }
            catch (Exception)
            {
                return null;
            }
        }

This works fine and pulls the data through but in a strange order. Where I would expect results of A B C D E F, it appears to be returning A C E B D F.

Could someone point out the error in my ways please?

Upvotes: 0

Views: 283

Answers (2)

MrDean
MrDean

Reputation: 3

Top, top man...thank you Ben. For anyone wanting the final code:

  return buyer.tblProducts 
.Where(p => p.MemberId == Memberkey) 
.Select(p => p.UnitUserfield1) 
.Distinct() 
.OrderBy(p => p) 
.ToArray();`

Upvotes: 0

Coding Flow
Coding Flow

Reputation: 21881

Do your OrderBy last, i have seen this before with LinqToSql, having the OrderBy before the Distinct caused it to generate SQL with no OrderBy in it.

Upvotes: 1

Related Questions