英文:
List<object>: how to apply a "where" for some index element?
问题
以下是翻译好的代码部分:
我有这种对象列表:
.AsEnumerable().Select(p => new List<object>()
{
p.ID,
p.DateActivity.HasValue ? p.DateActivity.Value.ToString("yyyyMMdd") : null,
p.DateActivity.HasValue ? p.DateActivity.Value.Month.ToString() : null,
p.DateActivity.HasValue ? p.DateActivity.Value.Year.ToString() : null,
p.Clinic.ID,
p.Clinic.Name,
p.Patient.LastName + " " + p.Patient.FirstName,
p.Patient.Gold,
p.Patient.PromoSmiles,
p.TreatmentType.Description ?? "",
p.PaymentMethod.Description ?? "",
p.ActivityPayments.Select((q, index) => new
{
Description = q.PaymentTypeID == (int)PaymentType.ACCONTO ? labelAcconto : q.PaymentTypeID == (int)PaymentType.SINGOLOPAGAMENTO && p.PaymentMethod.ID == 1 ? labelRata + (index - p.ActivityPaymentsNonSinglePaymentCount + 1) : labelImporto,
Amount = q.Amount,
Date = q.Date,
Paid = q.Paid
})
}).ToList();
最后一项(即data[11])是对象列表。
我想在 .ToList() 之前应用一个 Where,以便仅筛选data中至少有一个Paid = true的对象。如何做到这一点?
英文:
I've this kind of List of object:
.AsEnumerable().Select(p => new List<object>()
{
p.ID,
p.DateActivity.HasValue ? p.DateActivity.Value.ToString("yyyyMMdd") : null,
p.DateActivity.HasValue ? p.DateActivity.Value.Month.ToString() : null,
p.DateActivity.HasValue ? p.DateActivity.Value.Year.ToString() : null,
p.Clinic.ID,
p.Clinic.Name,
p.Patient.LastName + " " + p.Patient.FirstName,
p.Patient.Gold,
p.Patient.PromoSmiles,
p.TreatmentType.Description ?? "",
p.PaymentMethod.Description ?? "",
p.ActivityPayments.Select((q, index) => new
{
Description = q.PaymentTypeID == (int)PaymentType.ACCONTO ? labelAcconto : q.PaymentTypeID == (int)PaymentType.SINGOLOPAGAMENTO && p.PaymentMethod.ID == 1 ? labelRata + (index - p.ActivityPaymentsNonSinglePaymentCount + 1) : labelImporto,
Amount = q.Amount,
Date = q.Date,
Paid = q.Paid
})
}).ToList();
The last item (i.e. data[11]) is a list of objects.
I want to apply a Where before the .ToList(), so that it filters only objects of data where data[11] list have at least one Paid = true.
How can I do it?
答案1
得分: 0
只需在第一个Select
方法中使用匿名对象而不是对象列表。
var data = .AsEnumerable().Select(p => new
{
p.ID,
p.DateActivity.HasValue ? p.DateActivity.Value.ToString("yyyyMMdd") : null,
p.DateActivity.HasValue ? p.DateActivity.Value.Month.ToString() : null,
p.DateActivity.HasValue ? p.DateActivity.Value.Year.ToString() : null,
p.Clinic.ID,
p.Clinic.Name,
p.Patient.LastName + " " + p.Patient.FirstName,
p.Patient.Gold,
p.Patient.PromoSmiles,
p.TreatmentType.Description ?? "",
p.PaymentMethod.Description ?? "",
p.ActivityPayments.Select((q, index) => new
{
Description = q.PaymentTypeID == (int)PaymentType.ACCONTO ? labelAcconto : q.PaymentTypeID == (int)PaymentType.SINGOLOPAGAMENTO && p.PaymentMethod.ID == 1 ? labelRata + (index - p.ActivityPaymentsNonSinglePaymentCount + 1) : labelImporto,
Amount = q.Amount,
Date = q.Date,
Paid = q.Paid
})
}).ToList();
var filteredData = data.Where(x => x.ActivityPayments.Paid).ToList();
英文:
Just use an anonymous object instead of a list of objects in the first Select
method.
var data = .AsEnumerable().Select(p => new
{
p.ID,
p.DateActivity.HasValue ? p.DateActivity.Value.ToString("yyyyMMdd") : null,
p.DateActivity.HasValue ? p.DateActivity.Value.Month.ToString() : null,
p.DateActivity.HasValue ? p.DateActivity.Value.Year.ToString() : null,
p.Clinic.ID,
p.Clinic.Name,
p.Patient.LastName + " " + p.Patient.FirstName,
p.Patient.Gold,
p.Patient.PromoSmiles,
p.TreatmentType.Description ?? "",
p.PaymentMethod.Description ?? "",
p.ActivityPayments.Select((q, index) => new
{
Description = q.PaymentTypeID == (int)PaymentType.ACCONTO ? labelAcconto : q.PaymentTypeID == (int)PaymentType.SINGOLOPAGAMENTO && p.PaymentMethod.ID == 1 ? labelRata + (index - p.ActivityPaymentsNonSinglePaymentCount + 1) : labelImporto,
Amount = q.Amount,
Date = q.Date,
Paid = q.Paid
})
}).ToList();
var filteredData = data.where(x => x.ActivityPayments.Paid).ToList();
答案2
得分: 0
- 将
data[11]
强制转换为IEnumerable<dynamic>
:
.Where(data => ((IEnumerable<dynamic>)data[11])
.Any(q => q.Paid))
- 在
Select
之前应用Where
:
.AsEnumerable()
.Where(p => p.ActivityPayments.Any(q => q.Paid))
.Select(...)
英文:
Two solutions:
- Cast
data[11]
toIEnumerable<dynamic>
.Where(data => ((IEnumerable<dynamic>)data[11])
.Any(q => q.Paid))
- Apply
Where
beforeSelect
.AsEnumerable()
.Where(p => p.ActivityPayments.Any(q => q.Paid))
.Select(...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论