我有一个存储过程,该过程将从数据库中返回所有记录,我想使用linq过滤结果,但是问题是它只返回一个记录,尽管我要返回两个记录。
CustomerController
public class CustomerController : ApiController
{
List<CustomerModel> customers = new List<CustomerModel>();
private CustomerController()
{
var _data = ViewCustomer();
if (_data.Rows.Count > 0)
{
customers = (from DataRow _dr in _data.Rows
select new CustomerModel()
{
custId = Convert.ToInt32(_dr["custId"]),
custName = Convert.ToString(_dr["custName"]),
custAddress = Convert.ToString(_dr["custAddress"]),
custContact = Convert.ToString(_dr["custContact"]),
custStatus = Convert.ToString(_dr["custStatus"]),
agentId = Convert.ToInt32(_dr["agentId"]),
agentContact = Convert.ToString(_dr["agentContact"])
}).ToList();
}
}
// GET: api/Customer
public List<CustomerModel> Get()
{
return customers;
}
// GET: api/Customer/5
public CustomerModel Get(string num)
{
return customers.Where(x => x.agentContact.Equals(num)).FirstOrDefault();
}
DataRepository
namespace DataLibrary.Repository
{
public class DataRepository
{
public static DataTable ViewCustomer()
{
try
{
DataLayers CustData = new DataLayers();
CustData.cmdType = CommandType.StoredProcedure;
CustData.SQL = "CustomerStoredProcedure";
CustData.paramVal = new string[,]
{
{ "@custId", ""},
{ "@custName", ""},
{ "@custAddress", ""},
{ "@custContact", ""},
{ "@custStatus", ""},
{ "@agentId", ""},
{ "@statement", "Select"}
};
return CustData.GetData();
}
catch (Exception)
{
throw;
}
}
}
}
这是存储的过程。
create procedure CustomerStoredProcedure
(
custId int(11),
custName varchar(100),
custAddress varchar(100),
custContact varchar(20),
custStatus varchar(20),
agentId int(11),
statement varchar(30)
)
begin
if statement = 'Select' then
select c.custId, c.custName, c.custAddress, c.custContact, c.custStatus, c.agentId,
a.agentId, a.agentContact
from customer c
inner join agent a on c.agentId = a.agentId;
elseif statement = 'Insert' then
insert into customer ( custName, custAddress, custContact, custStatus, agentId)
values (custName, custAddress, custContact, custStatus, agentId);
elseif statement = 'Update' then
update customer set custName = custName, custAddress = custAddress,
custContact = custContact, custStatus = custStatus, agentId = agentId where
custtId = custId;
elseif statement = 'Delete' then
delete from customer where custId = custId;
end if;
这是我的DataLayers
public class DataLayers
{
public static string success = "Record successfully added!";
public static string failed = "Error: Data could not added, Please check your inputted data(s)!";
public CommandType cmdType { get; set; }
public string SQL { get; set; }
public string[,] paramVal { get; set; }
public static MySqlConnection Config()
{
var conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString.ToString());
conn.Open();
return conn;
}
public DataTable GetData()
{
MySqlCommand cmd = new MySqlCommand(this.SQL, Config());
cmd.CommandType = cmdType;
cmd.CommandTimeout = 0;
if (this.paramVal != null)
{
for (int i = 0; i <= paramVal.GetUpperBound(0); i++)
{
cmd.Parameters.AddWithValue(this.paramVal[i, 0].ToString().Trim(), this.paramVal[i, 1].ToString().Trim());
}
}
DataTable dt = new DataTable();
var dataAdapter = new MySqlDataAdapter(cmd);
dataAdapter.Fill(dt);
dataAdapter.Dispose();
cmd.Dispose();
Config().Close();
return dt;
}
public DataTable GetData(bool Error)
{
try
{
MySqlCommand cmd = new MySqlCommand(this.SQL, Config());
cmd.CommandType = cmdType;
cmd.CommandTimeout = 0;
if (this.paramVal != null)
{
for (int i = 0; i <= paramVal.GetUpperBound(0); i++)
{
cmd.Parameters.AddWithValue(this.paramVal[i, 0].ToString().Trim(), this.paramVal[i, 1].ToString().Trim());
}
}
DataTable dt = new DataTable();
var dataAdapter = new MySqlDataAdapter(cmd);
dataAdapter.Fill(dt);
dataAdapter.Dispose();
cmd.Dispose();
Config().Close();
return dt;
}
catch (Exception ex)
{
DataTable dt = new DataTable();
dt.Columns.Add("Error");
dt.Columns.Add("Message");
dt.Rows.Add("Error: ", ex.Message.Replace("Tables", "Table").Replace("MySql", "Database"));
return dt;
}
}
}
这是我的DataRepository
public class DataRepository
{
public static DataTable ViewAgent()
{
try
{
DataLayers AgentData = new DataLayers();
AgentData.cmdType = CommandType.StoredProcedure;
AgentData.SQL = "AgentStoredProcedure";
AgentData.paramVal = new string[,]
{
{ "@agentId", "" },
{ "@agentName", ""},
{ "@agentAddress", ""},
{ "@agentContact", ""},
{ "@agentStatus", ""},
{ "@catId", ""},
{ "@statement", "Select"}
};
return AgentData.GetData();
}
catch (Exception)
{
throw;
}
}
public static DataTable ViewCustomer()
{
try
{
DataLayers CustData = new DataLayers();
CustData.cmdType = CommandType.StoredProcedure;
CustData.SQL = "CustomerStoredProcedure";
CustData.paramVal = new string[,]
{
{ "@custId", ""},
{ "@custName", ""},
{ "@custAddress", ""},
{ "@custContact", ""},
{ "@custStatus", ""},
{ "@agentId", ""},
{ "@statement", "Select"}
};
return CustData.GetData();
}
catch (Exception)
{
throw;
}
}
}
返回所有记录
仅返回一条记录(错误的结果)
这是正确的记录结果
答案 0 :(得分:2)
这么多问题。
DataLayers
是什么,这是一些本地出产的数据访问库吗?@agentContact
上进行搜索?以此类推。但是,如果您坚持使用这种方法,那么您会根据需求定义错误的签名和逻辑。您要返回列表而不是单个项目:
public List<CustomerModel> Get(string num)
{
return customers.Where(x => x.agentContact.Equals(num)).ToList();
}