使用LINQ到转换列表< U>列出< T> [英] Using LINQ to convert List<U> to List<T>

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

问题描述

我有2类,它们有一些相同的属性。 我进的股票从1级列表的属性,而在那之后,我想采取一些必要的属性,并把它们纳入到第二类类型的列表。 我已经通过C#铸铁序列和运行正常,但我必须这样做使用LINQ。我试图做一些事情,但没有很好的效果。帮我请与建议。

第一类:

 公共类ServiceInfo {
    专用长_id;
    众长ID {
        {返回this._id; }
        集合{_id = value的; }
    }

    私人字符串_name;
    公共字符串名称{
        {返回this._name; }
        集合{_name =价值; }
    }

    专用长_qty;
    众长量{
        {返回this._qty; }
        集合{_qty =价值; }
    }

    私人双人_amount;
    公共双倍金额{
        {返回this._amount; }
        集合{_amount =价值; }
    }

    私人字符串_currency;
    公共字符串货币{
        {返回this._currency; }
        集合{_currency =价值; }
    }

    私营的DateTime? _日期;
    公开日期时间?日期 {
        {返回this._date; }
        集合{_date =价值; }
    }
}
 

第二类:

 类InvoiceWithEntryInfo {
    私人字符串currencyField;

    专用长IdField;
    众长IdIWEI {
        {返回this.IdField; }
        集合{IdIWEI =价值; }
    }

    私人字符串为nameField;
    公共字符串NameIWEI {
        {返回this.nameField; }
        集合{NameIWEI =价值; }
    }

    专用长qtyField;
    众长QuantityIWEI {
        {返回this.qtyField; }
        集合{QuantityIWEI =价值; }
    }

    私人双人amountField;
    公共双AmountIWEI {
        {返回this.amountField; }
        集合{AmountIWEI =价值; }
    }

    私营的DateTime的DateField;
    公开日期时间? DateIWEI {
        {返回this.dateField; }
        集合{DateIWEI =价值; }
    }

    公共字符串OwnerIWEI {
        得到;组;
    }
}
 

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

  VAR SIL =新的名单,其中,ServiceInfo>();
VAR iweil =新的名单,其中,InvoiceWithEntryInfo>();
 

...

 如果(SIL!= NULL)
    {
        的foreach(在SIL ServiceInfo项)
        {
            iweil.Add(新InvoiceWithEntryInfo
                {
                    IdIWEI = item.ID,
                    AmountIWEI = item.Amount,
                    DateIWEI = item.Date
                });
        }
 

LINQ样品至极犯规运行正常:

  iweilCOPY = sil.ConvertAll< InvoiceWithEntryInfo>(A =>(InvoiceWithEntryInfo)一);

            iweilCOPY = sil.FindAll(一个=>(SIL是InvoiceWithEntryInfo))ConvertAll&所述; InvoiceWithEntryInfo>(一个=>(InvoiceWithEntryInfo)一个);
 

解决方案

  VAR iweilCopy = sil.Select(项目=>新建InvoiceWithEntryInfo()
{
  IdWEI = item.Id,
  NameWEI = item.Name,
  ....
})了ToList()。
 

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.

1st Class:

   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; }
    }
}

2nd Class:

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# 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 sample wich doesnt 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到转换列表&LT; U&GT;列出&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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