【发布时间】:2015-05-08 10:55:15
【问题描述】:
我正在使用this library 在 Javascript 中获取 LINQ 功能。但是,我无法让 SingleOrDefault() 在我的代码中工作。它返回多个值。
这是我的 (AngularJS) 代码:
customerService.getCustomer(customerId)
.then(function (data) {
$scope.customer = data.data;
var primaryContact = Enumerable.From(data.data.contacts)
.SingleOrDefault(function (x) { return x.isPrimaryContact });
if (primaryContact) $scope.customer.address = primaryContact.address.addressLines;
$scope.customer.customerType = 'DEFAULT';
})
.catch(function (errorData) { alert("There has been an error retrieving the customer") });
即使我确定只有一个联系人满足条件,SingleOrDefault() 也会返回多个值。
有没有人使用过这个库并遇到过同样的问题?以下是正在使用的 XML 示例:
<contacts>
<notification/>
<address>
<notification/>
<addressId>400059994</addressId>
<addressLines>My Address
</addressLines>
<country>9000</country>
<county>East Sussex</county>
<postcode>BN12 1PP</postcode>
<town>Hove</town>
</address>
<contactId>400161871</contactId>
<emailAddress>myemail@test.com</emailAddress>
<isAccountsContact>false</isAccountsContact>
<isAirlineListContact>false</isAirlineListContact>
<isDefaultInvoicingContact>false</isDefaultInvoicingContact>
<isDeleted>false</isDeleted>
<isMarketingRegistered>true</isMarketingRegistered>
<isPrimaryBrokerContact>false</isPrimaryBrokerContact>
<isPrimaryContact>true</isPrimaryContact>
<telephoneNumber>01273 123123</telephoneNumber>
<website>www.fromthiscomesthat.co.uk</website>
</contacts>
<contacts>
<notification/>
<contactId>400161872</contactId>
<customerContactType>15000</customerContactType>
<forename>we</forename>
<isAccountsContact>false</isAccountsContact>
<isAirlineListContact>false</isAirlineListContact>
<isDefaultInvoicingContact>false</isDefaultInvoicingContact>
<isDeleted>false</isDeleted>
<isMarketingRegistered>true</isMarketingRegistered>
<isPrimaryBrokerContact>true</isPrimaryBrokerContact>
<isPrimaryContact>false</isPrimaryContact>
<surname>wew</surname>
<title>A Test Contract</title>
</contacts>
【问题讨论】:
-
你能用一个简短但完整的例子来说明这个问题吗? (一个简单的数组作为源数据就足够了......)
-
XML 在上面添加。如您所见,只有一个联系人的 isPrimaryContact 设置为 true。我刚刚注意到 SingleOrDefault() 可能根本不会返回任何结果,而不是返回多个联系人。这个库没有太多的文档...
-
我提出了一个简短但完整的示例——为什么要使用 XML 而不是更简单的数据源(例如数组)?这样你就可以把整个东西放到一个可运行的 sn-p...
-
它来自 AngularJS 服务调用。
-
我不明白为什么这会阻止您编写一个不需要 AngularJS 的简短但完整的示例...我看不出任何暗示该问题有任何关系使用 XML 或 AngularJS - 如果你发现它确实与这有关,同时你把一个简短但完整的例子放在一起,你就会学到一些非常重要的东西。
标签: javascript c# angularjs linq