I have two tables. One in one database and one in a separate database. I need to populate a dropdown list with options from the first, filtered by the second. I am using Linq-to-SQL. Below is how I pull the “un-filtered” list.
public static DataTable GetSPCCodeList()
{
using (var context = ProviderDataContext.Create())
{
IQueryable<tblProviderAdminSPCCode> tSPCCode = context.GetTable<tblProviderAdminSPCCode>();
return (tSPCCode
.Where(spcCode => spcCode.Inactive == null)
.OrderBy(spcCode => spcCode.SPCCodeID)
.Select(spcCode => new
{ spcCode.SPCCodeID, spcCode.SPCDescription,
spcCode.SPCCategoryID }))
.CopyLinqToDataTable();
}
}
The table I need to filter against simply contains a column for SPCCodeID. How would I go about filtering my List based on if they exist in the second table?
Generate and execute a LINQ query on the other database to get a collection of your
SPCCodeIDs into memory variable e.g.Then run your query but replace the Where clause like this