0
    var temp = (from assetVisit in db.AssetVisits
                join assetBundle in db.AssetBundles on assetVisit.AssetID equals assetBundle.AssetID
                join groupBundle in db.GroupBundles on assetBundle.BundleID equals groupBundle.BundleID
                join userGroup in db.UserGroups on groupBundle.GroupID equals userGroup.GroupID
                where assetVisit.CompanyID == companyID &&
                      userGroup.UserID == userID
                select new { AssetID = assetVisit.AssetID, Count = assetVisit.AccessCounter }).Distinct();

    IQueryable<Asset> final = (from t in temp
                              join asset in db.Assets on t.AssetID equals asset.AssetID
                              where asset.IsActive == true
                                    && asset.AssetTypeID == assetType
                                    && asset.ShowInResults == true
                                    && (asset.CompanyID == companyID || asset.CompanyID == -12081974)
                              orderby t.Count descending
                              select asset).Except(from companyAssets in db.Assets
                                            join copiedAssets in db.Assets on companyAssets.AssetID equals copiedAssets.OriginalAssetID
                                            where copiedAssets.CompanyID == companyID && companyAssets.CompanyID == -12081974 && copiedAssets.IsActive == true
                                            select companyAssets);


    return final.Take(limit);

OK so it's suppose to give back the assets in order based on t.Count but I think it might not be working because the .Count is actually not part of asset which is what is being selected, but I have no idea how to fix this.

As you can see there is an assetVisits table and an assets table, and I need to get back the assets in order of the assetVisits.AccessCount but I can't get it to work, what the hell??

1
  • 1
    dude I know it was 32% a few months ago when someone else told me that up until then I was always so conflicted because there were so many good answers, now I just pick the best one, don't worry I am in the habit now of always picking one!
    – MetaGuru
    Nov 30, 2009 at 21:09

2 Answers 2

8

You asked an almost identical question a couple of hours ago, and the answer is the same: do the ordering after you have selected the rows you want to order.

Change:

return final.Take(limit);

to:

var finalOrdered = from asset in final
    join assetVisit in db.AssetVisits on asset.AssetID equals assetVisit.AssetID
    orderby assetVisit.AccessCounter
    select asset;

return finalOrdered.Take(limit);

You can also remove the premature 'orderby' from your own code, since it is not doing anything.

6
  • no the last one included a lot less code and I never got it to work so I thought maybe my issue was the surrounding code, which I am realizing is true
    – MetaGuru
    Nov 30, 2009 at 21:12
  • No, the issue is exactly the same as before - you are again ordering too early. I've updated my post to include the code you need to change.
    – Mark Byers
    Nov 30, 2009 at 21:16
  • Then that is indeed a new question.
    – Mark Byers
    Nov 30, 2009 at 21:57
  • How can I add Distinct to that finalOrdered query as well as keep the order by if I am selecting the assets and but the order by value is in assetVisits??
    – MetaGuru
    Nov 30, 2009 at 22:01
  • 1
    If you need to order by something that is not selected then you can join with the extra tables you need to pull that field in, order by, then select again. But don't do distinct after doing that. If you don't know why you are getting duplicate records, perhaps you should look into that first. There might be an error in your data model, or a problem with the data in your database.
    – Mark Byers
    Dec 1, 2009 at 0:07
2

You query is missing the ordeby clause. You have one in a subquery, but placing orderby anywhere but on the outermost query expression is irelevant (except for when top is also used).

You have to specify the orderby on the outermost query.

3
  • I think this is exactly what I need but which one is the outermost query?
    – MetaGuru
    Nov 30, 2009 at 21:11
  • also how can I get the order by into the outermost query, how can I make it contain the t.Count variable to sort by? I can not figure this out for the life of me...
    – MetaGuru
    Nov 30, 2009 at 21:14
  • Technically the result of final.Take(limit) is the outermost query. However the Take operator (top) is an exception case (together with Skip), so you can have the order before the Take. Mark has lready gave you the answer to that. Nov 30, 2009 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.