Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

LINQ Query - Left Join Question

Posted on 2010-09-22
5
Medium Priority
?
448 Views
Last Modified: 2013-11-11
Hello all,

From my attached LINQ query I have here, I am having trouble where I only want to LEFT JOIN the CUSTGRP table to the CUST table so I can use in my where condition a field from the CUSTGRP table as shown.  How do I do this where I only return the DISTINCT CUST records but can filter out those based on the cust group filter I pass into it?  

I pass two param strings as you can see which one of them I filter the CUST table and the second I need to filter the CUSTGRP table so only I only pull the Customer records that have that condition and I join the two tables on CUSTID.  Again I only want the distinct records though from CUST only for the return results.

Hopefully this makes sense.  I tried messing around with the DefaultIfEmpty() but was always getting duplicate rows from CUST.

Thanks


public List<Customer> GetCustByName(string custRegionFragment, string grpFragment) {
            List<Customer> cf = new List<Customer>();

            using (CC = new CustModelEntities()) {
                    cf = (from CUST in CC.Customers
                          join CUSTGRP in CC.CustGroup on
                          CUST.CustId equals CUSTGRP.CustId
                             where CUST.CUSTLOC != null &&
                              (string.IsNullOrEmpty(custRegionFragment) ||
                               CUST.CUSTREGION.ToUpper().Contains(custRegionFragment.ToUpper())) &&
                              (string.IsNullOrEmpty(grpFragment) ||
                               CUSTGRP.GROUP.ToUpper().Contains(grpFragment.ToUpper()))
                            select CUST).ToList();
            }
           
            return cf;
        }

Open in new window

0
Comment
Question by:sbornstein2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:MikeToole
ID: 33742845
Can a customer appear in more than one group? If so, the Join will return duplicates - one per group.
0
 

Author Comment

by:sbornstein2
ID: 33791642
A customer will have multiple groups.  The user though is going to pass a fragment of a group name though such as 'GRPA" and there will be records that have GRPA1234 etc.

Customer ABC might belong to both GRPA1234 and another record GRPA5678.  I only want to pull that customer once.  Should I use a distinct or something?
0
 

Author Comment

by:sbornstein2
ID: 33791657
Sorry actually I cant use distinct because I need the whole customer records "select CUST" TOLIST.  
0
 
LVL 27

Accepted Solution

by:
MikeToole earned 2000 total points
ID: 33796928
You need to Group by customer.
The attached code is a trivial example.
It joins in the Where clause rather than explicitly using the Join statement, but that makes no difference to the logic.
L1 is populated with 5 rows, as you would expect from a Join.
L2 gets just 2 rows

        Dim cust = {1, 2, 3, 4}
        Dim cGroup = {2, 2, 4, 4, 4}
        Dim L1 = From c In cust, g In cGroup Where c = g Select c
        Dim L2 = From c In cust, g In cGroup Where c = g Group c By c Into Group

Open in new window

0
 

Author Closing Comment

by:sbornstein2
ID: 33885478
thank you.  this was great, sorry it took me a while to award this.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question