1
Answer

C# Linq Query Question

dahnealdo

dahnealdo

14y
1.7k
1

Good Evening everyone,

I've been trying to figure out the most efficient way to do this, but am falling short. Here's how it goes...

I am ultimately trying to determine "like customers" based on a specific customer's buying habits and a given threshold, say 50%. IE customer 1 purchased products A,B,C,D  ... customer 2 purchased B,C,D,E ... these two customers are >= 50% "likeness" so they should be matched.

My schema is as would be expected

CLIENT (1 ----- many)  CLIENT_PURCHASE (1 -------many) PRODUCT

*clientID                      *clientID *prodID                            *prodID


For now I am ignoring the threshold and simply am trying to find customers who have purchased any item within customer 1's history via a linq subquery. I think I have this working with the following two queries:


var clientOneHistory = (from cp in client.Client_Purchase                           
select cp.prodID).ToList();

  var matchedClients = (from cp in db.Client_Purchase
where clientOneHistory.Contains(cp.prodID)
select cp.Client.fullname).Distinct().ToList();


So my ultimate question is, "How do I work in the threshold portion?"

Thanks for your time
Answers (1)
0
Vulpes
NA 98.3k 1.5m 13y
You'll need to call Application.DoEvents() before Thread.Sleep(), otherwise the color changes will be cached by GDI+ and won't take effect until the method ends.

Here's how I'd do it. I'm assuming that the buttons all have different colors to start with and you want them to retain their colors when they are in sorted order. You don't need to deal with the case i == 5 as it will never be called:

        public void SwapColor(Button b1, Button b2)
        {
            Color tempColor = b1.BackColor;
            b1.BackColor = b2.BackColor;
            b2.BackColor = tempColor;
        }

        public void BubbleSort(int[] array, int Size)
        {
            int temp;

            for (int pass = 1; pass < Size; pass++)
            {
                for (int i = 0; i < Size - pass; i++)
                {
                    if (array[i] > array[i + 1])
                    {
                        if (i == 0)
                        {
                            SwapColor(btn1, btn2);
                        }
                        if (i == 1)
                        {
                            SwapColor(btn2, btn3);                       
                        }
                        if (i == 2)
                        {
                            SwapColor(btn3, btn4);
                        }
                        if (i == 3)
                        {
                            SwapColor(btn4, btn5);
                        }
                        if (i == 4)
                        {
                            SwapColor(btn5, btn6);
                        }
                       
                        Application.DoEvents();
                        Thread.Sleep(1000); // 2000 is a bit long 
                        temp = array[i];
                        array[i] = array[i + 1];
                        array[i + 1] = temp;
                    }
                }
            }
            a();
        }