使用 LINQ 转换 List<U>列出&lt;T&gt; [英] Using LINQ to convert List&lt;U&gt; to List&lt;T&gt;

查看:30
本文介绍了使用 LINQ 转换 List<U>列出&lt;T&gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个具有相同属性的类.我将第一类的属性放入列表中,然后,我想获取一些需要的属性并将它们放入第二类类型的列表中.我已经通过 C# 制作了演员序列并且运行正常,但我必须使用 LINQ.我试图做一些事情,但没有很好的结果.请帮助我提出建议.

I have 2 classes which have some identical properties. I stock into a list properties from 1st class, and after that, I want to take some needed properties and put them into a list of 2nd class type. I've made cast sequence through C# and that runs OK, but I must do with LINQ. I tried to do something but without good results. Help me please with suggestions.

第一类:

   public class ServiceInfo {
    private long _id;
    public long ID {
        get { return this._id; }
        set { _id = value; }
    }

    private string _name;
    public string Name {
        get { return this._name; }
        set { _name = value; }
    }

    private long _qty;
    public long Quantity {
        get { return this._qty; }
        set { _qty = value; }
    }

    private double _amount;
    public double Amount {
        get { return this._amount; }
        set { _amount = value; }
    }

    private string _currency;
    public string Currency {
        get { return this._currency; }
        set { _currency = value; }
    }

    private DateTime? _date;
    public DateTime? Date {
        get { return this._date; }
        set { _date = value; }
    }
}

二等:

class InvoiceWithEntryInfo {
    private string currencyField;

    private long IdField;
    public long IdIWEI {
        get { return this.IdField; }
        set { IdIWEI = value; }
    }

    private string nameField;
    public string NameIWEI {
        get { return this.nameField; }
        set { NameIWEI = value; }
    }

    private long qtyField;
    public long QuantityIWEI {
        get { return this.qtyField; }
        set { QuantityIWEI = value; }
    }

    private double amountField;
    public double AmountIWEI {
        get { return this.amountField; }
        set { AmountIWEI = value; }
    }
    
    private DateTime dateField;
    public DateTime? DateIWEI {
        get { return this.dateField; }
        set { DateIWEI = value; }
    }

    public string OwnerIWEI {
        get; set;
    }
}

运行正常的 C# 示例:...

C# sample which runs OK: ...

var sil = new List<ServiceInfo>();
var iweil = new List<InvoiceWithEntryInfo>();

...

if (sil != null)
    {
        foreach (ServiceInfo item in sil)
        {
            iweil.Add(new InvoiceWithEntryInfo
                {
                    IdIWEI = item.ID,
                    AmountIWEI = item.Amount,
                    DateIWEI = item.Date
                });
        }

无法正常运行的 LINQ 示例:

LINQ sample which doesn't run OK:

iweilCOPY = sil.ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);

iweilCOPY = sil.FindAll(a => (sil is InvoiceWithEntryInfo)).ConvertAll<InvoiceWithEntryInfo>(a => (InvoiceWithEntryInfo)a);

推荐答案

var iweilCopy = sil.Select(item => new InvoiceWithEntryInfo()
{
  IdWEI = item.Id,
  NameWEI = item.Name,
  ....
}).ToList();

这篇关于使用 LINQ 转换 List<U>列出&lt;T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆