I'm trying to return a query of all records from one table, after first performing a search into a different table. There's one common field in both tables. Here's the DDL describing the lookup table:
CREATE TABLE [dbo].[ASICodesSubstanceType](
[SubstanceType] [tinyint] NOT NULL,
[SubstanceDesc] [varchar](50) NOT NULL,
[DisplayOrder] [tinyint] NULL,
CONSTRAINT [PK_ASICodesSubstanceType] PRIMARY KEY CLUSTERED
(
[SubstanceType] ASC
)
and here's the DDL for the data table:
CREATE TABLE [dbo].[ASISubstanceUse](
[ClientNumber] [int] NOT NULL,
[CaseNumber] [tinyint] NOT NULL,
[Followup] [tinyint] NOT NULL,
[SubstanceType] [tinyint] NOT NULL,
[DaysUseLast30] [tinyint] NULL,
[LifeUseYears] [tinyint] NULL,
[Route] [tinyint] NULL,
[AgeOfFirstUse] [tinyint] NULL,
CONSTRAINT [PK_ASISubstanceUse] PRIMARY KEY NONCLUSTERED
(
[ClientNumber] ASC,
[CaseNumber] ASC,
[Followup] ASC,
[SubstanceType] ASC
)
Here's what I've done to retrieve all of the substances that were stored in the data table ASISubstanceUse table:
//get all of the currently selected substance types
var selectedSubstances = asiContext.ASISubstanceUses.Where(c => c.ClientNumber == ClientNumber && c.CaseNumber == CaseNumber)
.Select<ASISubstanceUse, byte>(r => r.SubstanceType);
But it's the next step I don't know how to do. I want to return all records from ASICodesSubstanceType, which match what I've got in selectedSubstances. Here's what I've got so far:
//get the records from the lookup table that match the selected substances
var selectedFromLookup = asiContext.ASICodesSubstanceTypes.Where(s => s.SubstanceType in selectedSubstances);
But this doesn't work at all. How do I do what I'm trying to do?
Use Any
:
var selectedFromLookup = asiContext.ASICodesSubstanceTypes
.Where(s => selectedSubstances.Any(sel => sel == s.SubstanceType));
Is this what you need?
int clientNumber = 0;
int caseNumber = 1;
var qry = from substance in ASICodesSubstanceTypes
join uses in ASISubstanceUses on substance.SubstanceType equals uses.SubstanceType
where uses.ClientNumber == clientNumber && uses.CaseNumber == caseNumber
select substance;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.