我如何制作linq

时间:2019-04-11 06:47:04

标签: javascript linq.js

enter image description here

 var adreees = Variables.PolicyCustomerAddresses;
 var linq= Enumerable.From(adreees)
                .Where(linq.CUSTOMER_ADRESS_ID === correspondenceAdress && linq.CUSTOMER_ID === customerCode)
                .DefaultIfEmpty(null)
                .First();

adreees是一个数组,它包含3个索引,我想获取CUSTOMER_ADRESS_ID === correspondenceAdress && linq.CUSTOMER_ID === customerCode索引的值。

1 个答案:

答案 0 :(得分:0)

您需要使用$作为实际数组的占位符,并且需要将条件写为字符串。

var adreees = [{ CUSTOMER_ADRESS_ID: 1, CUSTOMER_ID: 17, name: 'foo' }, { CUSTOMER_ADRESS_ID: 2, CUSTOMER_ID: 17, name: 'bar' }, { CUSTOMER_ADRESS_ID: 3, CUSTOMER_ID: 17, name: 'baz' }],
    correspondenceAdress = 2,
    customerCode = 17,
    result = Enumerable.From(adreees)
        .Where("$.CUSTOMER_ADRESS_ID === correspondenceAdress && $.CUSTOMER_ID === customerCode")
        .DefaultIfEmpty(null)
        .First();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>