簡體   English   中英

根據多個聯接條件從兩個自定義列表中選擇項目

[英]Select items from two custom lists based on multiple join criteria

我必須自定義數據列表:

    public class DatType1
{

    public string yr;
    public string qr;
    public string mt;
    public string cw;
    public string tar;
    public string rg;
    public string mac;
    public string fuel;
    public double fp;
    public double fi;
    public double fd;

}

public class DatType2
{

    public string yr;
    public string qr;
    public string mt;
    public string cw;
    public string tar;
    public string RG;
    public double pp;
    public double pi;
    public double fp;
    public double fi;
    public double fd;


}

如您所見,兩者之間有很多重疊之處。 我想將DatType1.fp,DatType1.fi,DatType1.fd的值添加到DateType2,但是我需要將它們放在正確的位置,正確的位置意味着一堆項目相等。

我在這里的網站上看了很多東西,但無法弄清楚。 我已經嘗試過……

from a in TableA
from b in TableB
where a.yr==b.yr & a.qr==b.qr & a.RG == b.RG & a.tar ==b.tar
select( r=> new DatType2{....}

然后在括號中重復我要保留的DateType2中的所有內容,並添加DatType1.fp,DatType1.fi,DatType1.fd。

如果我用蠻力做到這一點,我會做一個for循環,遍歷DatType1的每一行,看看我在DatType2中與一行匹配的位置,然后添加DatType1.fp,DatType1.fi,DatType1.fd-但這會非常慢

但是,這沒有用,而且遠遠不夠優雅! ... :)任何指針將不勝感激。

謝謝

我將創建一個具有所有常見數據的基類:

public class BaseClass    
{
    public string yr;
    public string qr;
    public string mt;
    public string cw;
    public string tar;
    public string rg;
    public double fp;
    public double fi;
    public double fd;

    public void SetAllValues(BaseClass Other)
    {
        this.yr = Other.yr;
        this.qr = Other.qr;
        //til the end.
        this.fd = Other.fd;
    }

    //with this you check the equal stuff...
    public bool HasSameSignature(BaseClass Other)
    { 
        if (this.yr != Other.yr) return false;
        if (this.qr != Other.qr) return false;  
        //all other comparisons needed
        return true;
    }

    //with this you set the desired ones:
    public void SetDesired(BaseClass Other)
    {
        this.fp = Other.fp;
        this.fi = Other.fi;
        //all other settings needed
    }
}    

其他人將繼承這個基礎:

public class DatType1 : BaseClass
{
    public string mac;
    public string fuel;
}


public class DatType2 : BaseClass
{
    public double pp;
    public double pi;
}

現在您可以使用a.HasSameSignature(b)代替a.yr==b.yr & a.qr==b.qr & a.RG == b.RG & a.tar ==b.tar 並設置值(或根據需要創建復制方法)。

我建議重載兩個對象的相等比較,以使DatType1:IEquatable<DatType2>和/或DatType2:IEquatable<DatType1> 那將允許您使用dat1.Equals(dat2)dat2.Equals(dat1)

或者,您可以實現一個比較委托並將其作為dat1.Equals(dat2,comparer)comparer.Compare(dat1,dat2) 如果您無法控制DatType1DatType2類型,則DatType1可能是理想的選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM