Naresh Seth

Naresh Seth

  • NA
  • 84
  • 1.8k

How to pass parameter to only one 'SELECT' ?

Jul 6 2018 2:11 PM
There is a stored proc as below with two selects. And i have to pass only one parameter (i.e Name ) to one SELECT through SQLQuery. How could i achieve that? 
 
Stored procedure :
 
"USE [CustomerDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetCustomers] @Name nvarchar(max), @ZipCode int
AS
SELECT [Id], [Name],[ZipCode] FROM [dbo].[Customers] WHERE [Name] LIKE (@Name);
SELECT [Id], [Name],[ZipCode] FROM [dbo].[Customers] WHERE [ZipCode] = @ZipCode;
GO"
 
public List GetCustomersByName(string name, int zipcode)
{
List ilst = new List();
using (CustomerContext cx = new CustomerContext())
{
var result = cx.Database.SqlQuery("GetCustomers. @Name", new SqlParameter("@Name", name));
foreach (Customer c in result)
{
Customer cust = new Customer();
cust.Name = c.Name;
cust.Zipcode = c.Zipcode;
ilst.Add(cust);
Console.WriteLine(cust.Name, cust.Zipcode);
}
}
return ilst;
}
 
 Please assist.

Answers (11)