0

嗨,我正在创建汽车租赁网络服务,但我需要将每个任务彼此分开。因此,我为客户提供了一项服务,它只是通用的名字、姓氏、年龄等。我还提供了一项雇用日期服务,其中包含雇用日期和雇用天数(稍后将用于计算成本)。

我的问题是试图链接 IHireDatesICustomers下面的(最终是汽车)是一个设置示例,我只是使用控制台应用程序来托管这些不同的服务来练习。

在此处输入图像描述

下面是我的 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;
            }
        }
    }
4

2 回答 2

0

除非您想编写自己的 LINQ 提供程序,否则您将不得不求助于使用 LINQ to Objects。这意味着您必须从两个服务中加载结果,然后执行连接。

我真的不认为这是一个好主意,我认为沿着这条路走下去会充满问题。

另一种方法是编写您自己的各种 rdbms(可能还有一个 LINQ 提供程序来包装它)。还有一条我不推荐的路线。

我想您可以拥有第三个服务来为其他两个服务提供数据,因此您可以将所有数据放在一个地方以便能够真正加入。

于 2012-07-25T02:19:37.260 回答
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 列表包含 HireDates 并且 HireDate 类具有属性 HireFromDate 和 HireToDate 和 CustomerID

于 2012-07-25T02:26:20.617 回答