I have a query that needs to retrieve a string value based on a function and it needs to join to another table using that string.
Background Info: I have one table that contains my company's data and one column in particular contains company id's (integers). I have another table that we import data into which contains our vendor data and their table has a list of company names. The problems are 1.) they don't have id's that match our unique id's and 2.) their company names don't exactly match our company names and 3.) I don't have control over either so I can't create a linking table at this time to join the data.
In order to solve these problems I attempted to create a function that looks like this:
private static string GetOtherCompName(int CompanyID)
{
return CompanyID == null ? "ERROR_COMPANY_NAME" :
CompanyID == 1 ? "AY COMPANY" :
CompanyID == 2 ? "BY COMPANY" :
CompanyID == 3 ? "CY COMPANY" :
CompanyID == 4 ? "XY COMPANY" : "ERROR_COMPANY_NAME";
}
In my linq query I attempted to do something like this:
var data =
from OurData in db.OUR_HEADER
join TheirData in db.THEIR_HEADER
on GetOtherCompName(OurData.CUSTOMER_NUM) equals TheirData.CUSTOMER_NAME
select new
{
STATE = TheirComp.STATE_CODE,
COMPANY_NAME = GetOtherCompName(OurData.CUSTOMER_NUM)
};
The SELECT new returns the correct result but the join gives me an error stating that it cannot translate into SQL. After further research on that error, I am told that I may need to change my function to an Expression Function but that didn't work and I think it's because I'm calling the function in the 'on' clause. How can I best achieve my solution?
Is there a way that I can do a join on a select of TheirData and in that select use my function to return the altered value--I'd have to change my on clause of course?
Thank You.