Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在where中的LINQ where_C#_Linq_Web Services_Boolean - Fatal编程技术网

C# 在where中的LINQ where

C# 在where中的LINQ where,c#,linq,web-services,boolean,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, carreg

所以我有点被下面的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>

1.
G
格拉姆
27
15.07.2012
29.07.2012
IAWB-OOTO
在我的客户方面,客户将“归还”车辆,对我来说,这基本上只是删除日期之间的租金,并从客户内部删除注册号。但是这里有一个问题,我必须用注册号来删除,所以我试图在where claus中做一个where,但它说我不能简单地将CarType转换为bool

我的web服务有以下列表:

        List<Customer> customermembers = new List<Customer>();
        List<HireDate> hiredates = new List<HireDate>();
        List<CarType> cartypes = new List<CarType>();
List customermembers=new List();
List hiredates=新列表();
列表类型=新列表();

我只是想,
客户成员,其中客户汽车类型的重新登记号等于mystring
就可以了?

因为您在if块中实际上没有使用
结果
,您可以这样做:

    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

但是,这并不是非常有效,因为您要对列表进行三次迭代;您应该只迭代一次。我想发布一个示例,但我认为您的代码中有一个bug,我不想重复。考虑单独的客户与单独的注册与相同的HIRE日期和最新日期。您的代码将从尚未归还车辆的客户中删除CustomerHireDate

但是,您很可能打算在if块中使用
result
,这将修复bug:

    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); 
        } 
    }

请记住,
其中
不会更改原始集合,因此您需要迭代
结果
,以获得过滤后的对象集。

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

    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

但是,这并不是非常有效,因为您要对列表进行三次迭代;您应该只迭代一次。我想发布一个示例,但我认为您的代码中有一个bug,我不想重复。考虑单独的客户与单独的注册与相同的HIRE日期和最新日期。您的代码将从尚未归还车辆的客户中删除CustomerHireDate

但是,您很可能打算在if块中使用
result
,这将修复bug:

    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
,以获得过滤后的对象集