CREATE TABLE [DBO].[TBL_PROFILE]
(
PROFILE_ID BIGINT IDENTITY(1, 1) NOT NULL
,NAME NVARCHAR(200) NOT NULL
,GENDER TINYINT NOT NULL
,CASTE NVARCHAR(15)
,QUALIFICATION NVARCHAR(50)
,COMPLEXION NVARCHAR(50)
,FEEDING_HBTS INT
,CONSTRAINT PK__PROFILE PRIMARY KEY (PROFILE_ID)
)
INSERT INTO [DBO].[TBL_PROFILE]
VALUES ('AJAY', 1, 'BRAHMAN', 'MASTER', 'FAIR', 1),
('JIRAM', 1, 'CHETTRI', 'BACHELOR', 'BLACK', 1),
('SUMAN', 1, 'NEWAR', '+2', 'BLACK', 1),
('HIRA', 1, 'MAGAR', 'BACHELOR', 'FAIR', 1),
('JANNY', 1, 'MAGAR', 'BACHELOR', 'MEDIUM', 1),
('RANVI', 1, 'NEWAR', 'BACHELOR', 'BLACK', 1),
('SURAJ', 1, 'BRAHMAN', 'BACHELOR', 'FAIR', 1);
Above is the SQL Server table and some sample data for testing purpose.
In the front end, the user has the option to select the caste, qualification, complexion and based on these conditions I've to design the query. The user can select more than one value for any of these attributes. Now based on these user conditions, I've to design the sql query in C#.
Suppose, the user selects NEWAR, MAGAR as caste and others are NULL, then the query would be:
SELECT *
FROM [DBO].[TBL_PROFILE]
WHERE GENDER = 1
AND CASTE IN ('NEWAR', 'MAGAR')
Suppose the user selects qualification as BACHELOR and COMPLEXION as FAIR, BLACK then the query would be:
SELECT *
FROM [DBO].[TBL_PROFILE]
WHERE GENDER = 1
AND COMPLEXION IN ('FAIR', 'BLACK')
AND QUALIFICATION = 'BACHELOR';
So the query is dynamic, based on the three attributes. How can I write dynamic query in C# for this scenario?