您好我正在创建一个租车网络服务,但我需要保持每个任务彼此独立。所以我为客户提供了一个通用的名字,姓氏,年龄等服务。我还提供了一个租用日期服务,该服务有租用日期和租用天数(稍后将用于计算)成本)。
我的问题是尝试链接 IHireDates
到ICustomers
(以及最终汽车)下面的设置示例我只是使用控制台应用来托管这些不同的服务实践。
以下是我的ICustomers和IHireDate服务我试图将IHireDate发送给客户,但我不知道该怎么做:
ICustomer(服务器端)
namespace WcfServiceLibrary3
{
#region Customer DataContract
[DataContract(Name = "Customers")]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[Serializable]
public class Customers : ICustomers
{
[DataMember(Name = "CustomerID")]
public string CustomerID { get; set; }
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
[DataMember(Name = "Age")]
public string Age { get; set; }
IHireDates(服务器端):
namespace WcfServiceLibrary3
{
#region HireDate DataContract
[DataContract(Name = "HireDate")]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[Serializable]
public class HireDate : IHireDate
{
[DataMember(Name = "HireFromDate")]
public string HireFromDate { get; set; }
[DataMember(Name = "HireToDate")]
public string HireToDate { get; set; }
[DataMember(Name = "NumberOfDaysToHire")]
public string NumberOfDaysToHire { get; set; }
#endregion
#region Lists
List<HireDate> HireDates = new List<HireDate>();
#endregion
public void ICustomers.AddCustomerHireDates(string hirefromdate, string hiretodate, string customerID, string firstname, string lastname)
{
// not sure this will work
var result = ICustomers.Where(n => String.Equals(n.CustomerID, customerID)).FirstOrDefault();
if (result != null)
{
result.HireDates.Add(new ICustomers() { CustomerID = customerID, FirstName = firstname, LastName = lastname });
}
}
例如,我不确定这是否正确ICustomers.AddCustomerHireDates
和ICustomers.Where
表示客户没有定义哪里。真的不知道怎么做这个交叉LINQ服务,如果有人可以帮助我会非常感激。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.ServiceModel;
namespace WcfServiceLibrary3
{
#region Customer DataContract
[DataContract(Name = "Customers")]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[Serializable]
public class Customers : ICustomers
{
[DataMember(Name = "CustomerID")]
public string CustomerID { get; set; }
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
[DataMember(Name = "Age")]
public string Age { get; set; }
#endregion
#region Lists
List<Customers> customermembers = new List<Customers>();
int eCount = 0;
#endregion
public void AddCustomer(Customers customer)
{
customer.CustomerID = (++eCount).ToString();
customermembers.Add(customer);
//BinaryFormatter f = new BinaryFormatter();
//Stream w = new FileStream("c:\\list.dat", FileMode.Append);
//f.Serialize(w, staff);
//w.Close();
}
public void RemoveCustomer(string customerID)
{
customermembers.RemoveAll(s => s.CustomerID == customerID);
}
public List<Customers> GetCustomerCollection()
{
return customermembers;
}
public void UpdateCustomer(string customerID, Customers customer)
{
var findCustomerMember = customermembers.Where(s => s.CustomerID == customerID).FirstOrDefault();
if (findCustomerMember != null)
{
findCustomerMember.CustomerID = customer.CustomerID;
findCustomerMember.FirstName = customer.FirstName;
findCustomerMember.LastName = customer.LastName;
//findStaffMember.TimeAdded = staff.TimeAdded;
//BinaryFormatter f = new BinaryFormatter();
//Stream w = new FileStream("c:\\updatelist.dat", FileMode.Append);
//f.Serialize(w, staff);
//w.Close();
}
}
public List<Customers> GetCustomer(string anything)
{
List<Customers> customers = customermembers.Where(n =>
string.Equals(n.CustomerID, anything, StringComparison.CurrentCultureIgnoreCase)
|| string.Equals(n.FirstName, anything, StringComparison.CurrentCultureIgnoreCase)
|| string.Equals(n.LastName, anything, StringComparison.CurrentCultureIgnoreCase)
|| string.Equals(n.Age, anything, StringComparison.CurrentCultureIgnoreCase)
).ToList();
return customers;
}
}
}
答案 0 :(得分:0)
除非您想编写自己的LINQ提供程序,否则您将不得不求助于使用LINQ to Objects。这意味着你必须从两个服务加载结果并且hen执行连接。
我真的不认为这是一个好主意,我认为沿着这条路走下去将充满问题。
替代方法是编写自己的rdbms of sort(可能还有一个LINQ提供程序来包装它)。还有一条我不推荐的路线。
我认为您可以使用第三个服务来为其他两个服务提供数据,因此您可以将所有数据放在一个位置,以便能够真正加入。
答案 1 :(得分:0)
public Customer GetCustomerByHireDates(string hirefromdate, string hiretodate)
{
var customerID = HireDates.Where(h => h.HireFromDate == hirefromdate && h.HireToDate == hiretodate).Select(sl=>sl.CustomerID).FirstOrDefault();
Customers customer = null;
var customers = new Customers().GetCustomerCollection();
if (customers != null)
{
customer = customers.Where(c=>c.CustomerID==customerID).FirstOrDefault();
}
return customer;
}
我希望HireDate类中的HireDates List包含HireDates,HireDate类也有HireFromDate和HireToDate以及CustomerID
属性