Javascript 如何通过运行时生成的属性WebAPI匹配两个具体类?
这是我工作的延伸 我必须将CorelationID传递给我的API服务,并匹配部门和员工记录 我从Javascript传递的数组如下 EmployeesAPIController.csJavascript 如何通过运行时生成的属性WebAPI匹配两个具体类?,javascript,c#,asp.net-web-api,Javascript,C#,Asp.net Web Api,这是我工作的延伸 我必须将CorelationID传递给我的API服务,并匹配部门和员工记录 我从Javascript传递的数组如下 EmployeesAPIController.cs using System.Web.Http; namespace EmployeeService { [RoutePrefix("Employee")] public class EmployeesAPIController : ApiController
using System.Web.Http;
namespace EmployeeService
{
[RoutePrefix("Employee")]
public class EmployeesAPIController : ApiController
{
[HttpPost]
[Route("InsertEmployeeDepartment")]
public EmployeeDepartment InsertEmployeeAndDepartment(object employeedepartment)
{
var empDept = JsonConvert.DeserializeObject<EmployeeDepartment>(employeedepartment.ToString());
//insert the new department records
DepartmentRecords = InsertDepartment(empDept.Departments);
//fill the employee record with the associated Coreration Id
Here I have to join the Employee and Department Records by the CorelationID
//insert employee records
EmployeeRecords = InsertEmployee(empDept.Employees);
}
}
}
我怎么做
对象看起来像(示例)
编辑
我正在尝试使用Expando对象,就像现在一样
公共员工部门插入员工和部门(扩展对象员工)
如何从这里获取员工和部门数组?如果我正确理解了您的问题,您只需在correlationID上使用联接即可获取部门列表中具有匹配correlationID的员工
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var employees = new List<Employee> { new Employee {Name = "MY_NAME1", CorrelationID = "CID1"}, new Employee {Name = "MY_NAME2", CorrelationID = "CID2"}, new Employee {Name = "MY_NAME3", CorrelationID = "CID3"}};
var depts = new List<Department> { new Department {Name = "DEPT_NAME1", CorrelationID = "CID2"}, new Department {Name = "DEPT_NAME2", CorrelationID = "CID1"}};
var joined = employees.Join(depts, x => x.CorrelationID, x => x.CorrelationID, (employee, dept) => new {EmployeeName = employee.Name, DepartmentName = dept.Name});
foreach(var pair in joined){
Console.WriteLine("EmployeeName: {0}, DepartmentName: {1}", pair.EmployeeName, pair.DepartmentName);
}
}
public class Employee
{
public string Name {get;set;}
public string CorrelationID {get;set;}
}
public class Department
{
public string Name {get;set;}
public string CorrelationID {get;set;}
}
public class SqlBuilder{
private static string _baseSql = "select * from customers";
public static string GetSql(string customerId){
var final = _baseSql;
if(customerId != null)
final += " WHERE custId=@custId";
return final;
}
}
}
部门记录是什么样子的?插入后CorrelationId将更改?它取决于插入类型。如果成功,则相关性将保持不变,否则会发生变化。基于此,我需要匹配员工,并且只有具有匹配CorrelationId的员工才有资格获得有效的插入。我在回答中输入了此查询,该查询将只接受匹配
CorrelationId
的员工。请参阅,员工没有CorelationId,也没有部门。这是动态创建的。有什么原因不能将它们添加到对象的定义中,并在其余时间忽略它们?C#不能很好地处理动态生成的参数。在序列化过程中,关联对象将被忽略。是。。。我们需要在客户端创建关联id…它不能是域模型的一部分。。我们需要根据服务器端的相关id匹配记录。我可以想到两种解决方案。一种是使用Json.Decode(employeeDepartment),它将返回一个动态对象,您可以从中创建值。另一种方法是创建一个新的域模型,其中包含CorrelationID,仅用于反序列化目的。
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var employees = new List<Employee> { new Employee {Name = "MY_NAME1", CorrelationID = "CID1"}, new Employee {Name = "MY_NAME2", CorrelationID = "CID2"}, new Employee {Name = "MY_NAME3", CorrelationID = "CID3"}};
var depts = new List<Department> { new Department {Name = "DEPT_NAME1", CorrelationID = "CID2"}, new Department {Name = "DEPT_NAME2", CorrelationID = "CID1"}};
var joined = employees.Join(depts, x => x.CorrelationID, x => x.CorrelationID, (employee, dept) => new {EmployeeName = employee.Name, DepartmentName = dept.Name});
foreach(var pair in joined){
Console.WriteLine("EmployeeName: {0}, DepartmentName: {1}", pair.EmployeeName, pair.DepartmentName);
}
}
public class Employee
{
public string Name {get;set;}
public string CorrelationID {get;set;}
}
public class Department
{
public string Name {get;set;}
public string CorrelationID {get;set;}
}
public class SqlBuilder{
private static string _baseSql = "select * from customers";
public static string GetSql(string customerId){
var final = _baseSql;
if(customerId != null)
final += " WHERE custId=@custId";
return final;
}
}
}
> EmployeeName: MY_NAME1, DepartmentName: DEPT_NAME2
> EmployeeName: MY_NAME2, DepartmentName: DEPT_NAME1