LINQ在哪里

时间:2012-07-26 23:14:50

标签: c# linq web-services boolean

所以我坚持下面的linq声明,

        public void ReturnHire(string carregistration, string hirefromdate, string hiretodate)
        {
            var result = customermembers.Where(c => c.CustomerCarType.Where(n => String.Equals(n.CarRegistration, carregistration)).FirstOrDefault(); // this line
            if (result != null)
            {
                customermembers.ForEach(c => c.CustomerHireDate.RemoveAll(cs => cs.HireFromDate == hirefromdate && cs.HireToDate == hiretodate));
                customermembers.ForEach(c => c.CustomerCarType.RemoveAll(cs => cs.CarRegistration == carregistration));
            }
        }

我的xml看起来像这样:

<ArrayOfCustomer>
    <Customer>
        <CustomerID>1</CustomerID>
        <FirstName>G</FirstName>
        <LastName>Graam</LastName>
        <Age>27</Age>
        <CustomerHireDate>
            <HireDate>
                <HireFromDate>15.07.2012</HireFromDate>
                <HireToDate>29.07.2012</HireToDate>
            </HireDate>
        </CustomerHireDate>
        <CustomerCarType>
            <CarType>
                <CarRegistration>IAWB-OOTO</CarRegistration>
            </CarType>
        </CustomerCarType>
    </Customer>
</ArrayOfCustomer>

在我的客户端,客户将“退回”车辆,对我来说,这基本上只是取消了日期之间的租用,并从客户中删除了注册号码。但是这里是我必须删除的注意事项只有注册号,所以我试图在where where claus做一个地方,但它说我无法将CarType转换为bool。

我的网络服务有以下列表:

        List<Customer> customermembers = new List<Customer>();
        List<HireDate> hiredates = new List<HireDate>();
        List<CarType> cartypes = new List<CarType>();

我只是觉得customermembers where customer car type where rehistration number equals mystring会没事的?

1 个答案:

答案 0 :(得分:1)

由于您在if块中实际上没有使用result,因此可以执行此操作:

    public void ReturnHire(string carregistration, string hirefromdate, string hiretodate) 
    { 
        if (customermembers.Any(c => c.CustomerCarType.Any(n => String.Equals(n.CarRegistration, carregistration)))
        { 
            customermembers.ForEach(c => c.CustomerHireDate.RemoveAll(cs => cs.HireFromDate == hirefromdate && cs.HireToDate == hiretodate)); 
            customermembers.ForEach(c => c.CustomerCarType.RemoveAll(cs => cs.CarRegistration == carregistration)); 
        } 
    } 
如果至少有一个元素满足谓词,则

Any返回true。

然而,这并不是非常有效,因为你将列表重复三次;你应该只迭代一次。我会发一个例子,但我认为你的代码中有一个错误,我不想重复它。考虑使用相同的hirefromdate和hiretodate进行单独注册的单独客户。您的代码将从尚未退回汽车的客户处删除CustomerHireDate。

但是,您可能打算在if块中使用result,这应该可以修复错误:

    public void ReturnHire(string carregistration, string hirefromdate, string hiretodate) 
    { 
        var result = customermembers.Where(c => c.CustomerCarType.Any(n => String.Equals(n.CarRegistration, carregistration));
        foreach (var customermember in result)
        { 
            customermember.CustomerHireDate.RemoveAll(cs => cs.HireFromDate == hirefromdate && cs.HireToDate == hiretodate); 
            customermember.CustomerCarType.RemoveAll(cs => cs.CarRegistration == carregistration); 
        } 
    }

请记住,Where不会更改原始集合,因此您需要迭代result以获取已过滤的对象集。