views:

59

answers:

1

Hello all.

I am struggling once again so any help would be gratefully received.

I have the following LINQ that pulls back a list of data:

public static List<tblWeight> GetWeights(string memberid, string locationid, string buyer, string subcategory, string product)
        {

            MyEntity getweights = new MyEntity ();

            var r = (from p in getweights.tblWeights
                     where p.MemberId == memberid &&
                              p.LocationId == locationid
                     select p);

            if (buyer != "Not Specified")
                r = r.Where(p => p.UnitUserField1 == buyer);

            if (subcategory != "Not Specified")
                r = r.Where(p => p.UnitUserField2 == subcategory);

            if (product != "Not Specified")
                r = r.Where(p => p.IDDesc == product);

            return r.ToList();
        }

Lovely!

What I would like to do now is based upon this result set and the unit IDs (IDDesc), I then go to tblPurchase, pull back a few columns from tblPurchases and group the columns.

So for example, we have tblWeight looking like so:

MemberID    LocationID  Buyer   SubCategory IDDesc
1            1           Cat1   Sub1        ab
1            1           Cat1   Sub1        abc
1            1           Cat1   Sub2        abcd

The user makes a search for Sub1 in subcategory and the above LINQ does the trick and pulls back the first two rows from above. Fine.

What I need the LINQ to do now is to go to tblPurchases:

MemberID    LocationID  IDDesc  SupplierID  SupplierStatus
1           1          ab        Sup1            Live
1           1          abc       Sup1            Live
1           1          abcd      Sup2            Dead

And then pull back the following result so it is joined on MemberID, LocationID and IDDesc but just selects tblPurchases.

Sup1 Live (or all columns in tblPurchases, just grouped/distinct)

I have tried to add in a join and on but no matter how many different variations, I still come across the red squiggle of doom!!!

If anyone can help, beer/kiss is on offer again.

A: 

The following LINQ query should do what you want:

var result = from w in tblWeight
             where w.SubCategory == "Sub1"
             join p in tblPurchases on
                new { w.MemberID, w.LocationID, w.IDDesc } equals
                new { p.MemberID, p.LocationID, p.IDDesc }
             group p by new { p.SupplierID, p.SupplierStatus } into pg
             select pg.Key;

The variable result is a list containing tuples of SupplierID and SupplierStatus.

If you also want to put the conditional parts in there, it gets a little more complicated. Here's how to do it:

var weights = from w in tblWeight
              select w;
weights = weights.Where(w => w.SubCategory == "Sub1");
// You can add additional where clauses here.

// Now join with tblPurchases and group by SupplierID and SupplierStatus.
var result =
    weights.Join(tblPurchases,
                 w => new { w.MemberID, w.LocationID, w.IDDesc },
                 p => new { p.MemberID, p.LocationID, p.IDDesc },
                 (w, p) => p)
           .GroupBy(p => new { p.SupplierID, p.SupplierStatus },
                    (k, ps) => new
                                {
                                    k.SupplierID,
                                    k.SupplierStatus,
                                    TotalQty = ps.Sum(p => p.PurchaseQty)
                                });
Ronald Wildenberg
Thanks for looking into this for me Ronald - top stuff so far. I'm optimistic with the "yet" part of your answer. it's the conditional where part that has really thrown me.
Ricardo Deano
I added additional stuff that allows you to do the conditional where(s) and also join and group by. I'll skip the kisses but shall I leave you an address to send the beer to ;)
Ronald Wildenberg
*Insert expletives here* I would never have come close to getting this!!! Genius Ronald...if it works ; ) Thanks for looking into this for me.
Ricardo Deano
You're welcome :) If this answers your question, could you mark it as answered? Thanks.
Ronald Wildenberg
Ronald - sorry to bother you again. I had another column, PurchaseQty for each IDDesc in tblPurchases, how would I amned the above code so that this aggregate function is also taking place? apologies for the amount of questions.
Ricardo Deano
Updated my answer to include `PurchaseQty`.
Ronald Wildenberg
Legend. That is fantastic. I owe you more beer/kisses!!
Ricardo Deano
Please, no kisses ;) Beer would be appreciated.
Ronald Wildenberg