views:

39

answers:

2

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?

+1  A: 

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.

Ben Robinson
I have tried that but when implementing this, I receive " 'string' does not contain a definition for 'UnitUserfield1' and no extension method 'Unitiuserfield1' accepting a first argument of type 'string' could be found" error. I'm not sure how to tackle that bad boy.
MrDean
Your dealing with an IEnumerable<string> collection by then so just do .OrderBy(p => p);
Ben Robinson
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();
MrDean
A: 

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();`
MrDean