我试图弄清楚为什么此LINQ查询(C#)无法按预期工作:
import json
import os
from sendgrid.helpers.stats import *
from sendgrid import *
# NOTE: you will need move this file to the root directory of this project to execute properly.
# Assumes you set your environment variable:
# https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key
sg = SendGridAPIClient(os.environ.get('SG****'))
def pprint_json(json_raw):
print(json.dumps(json.loads(json_raw), indent=2, sort_keys=True))
def build_global_stats():
global_stats = Stats()
global_stats.start_date = '2020-01-13'
global_stats.end_date = '2020-01-14'
global_stats.aggregated_by = 'day'
return global_stats.get()
def get_global_stats():
stats_params = build_global_stats()
response = sg.client.stats.get(query_params=stats_params)
print(response.status_code)
print(response.headers)
pprint_json(response.body)
get_global_stats()
Traceback (most recent call last):
File "<ipython-input-10-cee8ef5434a2>", line 35, in <module>
get_global_stats()
File "<ipython-input-10-cee8ef5434a2>", line 29, in get_global_stats
response = sg.client.stats.get(query_params=stats_params)
File "C:\Users\blah\AppData\Local\Continuum\anaconda3\lib\site-packages\python_http_client\client.py", line 262, in http_request
self._make_request(opener, request, timeout=timeout)
File "C:\Users\blah\AppData\Local\Continuum\anaconda3\lib\site-packages\python_http_client\client.py", line 178, in _make_request
raise exc
ForbiddenError: HTTP Error 403: FORBIDDEN
PS我更正了属性的名称,它们令人误解,对不起。
假设对于某些人// If the user receives 3 salaries, take the first, if not, show as 100
var salary = populationData
.Select(
x => x.AdultData?
.TaxPayerData?
.Where(x => x.Salary.Count == 3)
.Select(x => x.Value).FirstOrDefault()
)
.DefaultIfEmpty(100)
.ToList();
和IsAdult = false
,对于那些人,我需要TaxPayer = null
或我选择的任何其他值,但不需要DefaultIfEmpty = 100
。目前,我的结果是null
。
有什么想法吗?
答案 0 :(得分:1)
您可以尝试这种方式
// If the user receives 3 salaries, take the first, if not, show as 100
var salary = populationData
.Select(
x => {
var result = x.AdultData?
.TaxPayerData?
.Where(x => x.Salary.Count == 3)
.Select(x => x.Value).FirstOrDefault()
return result ?? 100;}
)
.ToList();