I have 2 tables, table 1 has a field which contains prefixes for companies.
The values retrieved from table 1 need to be used to get records from table 2 (based on the prefixes returned).
I coded below to do a startswith to demo what I need done, however what I need to do is build a dynamic starswith clause.
I know the code below wont compile, just putting it out there to show what I would like to accomplish.
//* This pulls list of prefixes from Table 1
var xrfPrefixes = from xx in XrfPrefixes
select new
{
xxx.CompanyPrefix
};
//* Get list of prefixes
string PrefixList = "";
foreach (var xxx in xrfJobPrefix)
{
PrefixList = PrefixList + xxx.FirmJobPrefix + ",";
}
//* Get back Matching records from table 2
var results = from p in Orders.Where(p => p.OrdCust.StartsWith(PrefixList))
select p;
Sample Data in Table 1 (Customers):
- Row 1: Customer_Prefix char(50) value = “ab1, ab2”
- Row 2: Customer_Prefix char(50) value = “xy1”
- Row 3: Customer_Prefix char(50) value = “xy2”
Sample data in table 2 (orders):
- row 1: customer_name char(50) value = “ab1filler”
- row 2: customer_name char(50) value = “ab2filler”
- row 3: customer_name char(50) value = “ab3filler”
- row 4: customer_name char(50) value = “xy1filler”
- row 5: customer_name char(50) value = “xy2filler”
- row 6: customer_name char(50) value = “xy3filler”
Based on the customer_prefixes in the Customers table, the records from Orders Table returned would be Rows 1,2,4 & 5
Create a PrefixList. Don’t do it as a list of comma-separated strings, do it as a real
List<String>. That means you have to split comma-separated values (usingString.Split, for example):After you have a PrefixList, you can do a simple LINQ query as follows: