List: 如何为某个索引元素应用“where”条件?
huangapple go评论62阅读模式
英文:

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 =&gt; new List&lt;object&gt;()
{
	p.ID,
	p.DateActivity.HasValue ? p.DateActivity.Value.ToString(&quot;yyyyMMdd&quot;) : 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 + &quot; &quot; + p.Patient.FirstName,
	p.Patient.Gold,
	p.Patient.PromoSmiles,
	p.TreatmentType.Description ?? &quot;&quot;,
	p.PaymentMethod.Description ?? &quot;&quot;,
	p.ActivityPayments.Select((q, index) =&gt; new
	{
		Description = q.PaymentTypeID == (int)PaymentType.ACCONTO ? labelAcconto : q.PaymentTypeID == (int)PaymentType.SINGOLOPAGAMENTO &amp;&amp; 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 =&gt; new
{
    p.ID,
    p.DateActivity.HasValue ? p.DateActivity.Value.ToString(&quot;yyyyMMdd&quot;) : 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 + &quot; &quot; + p.Patient.FirstName,
    p.Patient.Gold,
    p.Patient.PromoSmiles,
    p.TreatmentType.Description ?? &quot;&quot;,
    p.PaymentMethod.Description ?? &quot;&quot;,
    p.ActivityPayments.Select((q, index) =&gt; new
    {
        Description = q.PaymentTypeID == (int)PaymentType.ACCONTO ? labelAcconto : q.PaymentTypeID == (int)PaymentType.SINGOLOPAGAMENTO &amp;&amp; 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 =&gt; x.ActivityPayments.Paid).ToList();

答案2

得分: 0

  1. data[11] 强制转换为 IEnumerable&lt;dynamic&gt;
.Where(data =&gt; ((IEnumerable&lt;dynamic&gt;)data[11])
                .Any(q =&gt; q.Paid))
  1. Select 之前应用 Where
.AsEnumerable()
.Where(p =&gt; p.ActivityPayments.Any(q =&gt; q.Paid))
.Select(...)
英文:

Two solutions:

  1. Cast data[11] to IEnumerable&lt;dynamic&gt;
.Where(data =&gt; ((IEnumerable&lt;dynamic&gt;)data[11])
                .Any(q =&gt; q.Paid))
  1. Apply Where before Select
.AsEnumerable()
.Where(p =&gt; p.ActivityPayments.Any(q =&gt; q.Paid))
.Select(...)

huangapple
  • 本文由 发表于 2023年2月6日 19:13:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码