[英]Select items from two custom lists based on multiple join criteria
I have to custom data lists: 我必须自定义数据列表:
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;
}
and 和
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;
}
As you can see there is a lot of overlap between the two. 如您所见,两者之间有很多重叠之处。 I would like to add the values for DatType1.fp, DatType1.fi, DatType1.fd to DateType2 but I need to put them in the right place, the right place means where a bunch of the items are equal. 我想将DatType1.fp,DatType1.fi,DatType1.fd的值添加到DateType2,但是我需要将它们放在正确的位置,正确的位置意味着一堆项目相等。
I have looked a lot on the site here but couldnt figure it out. 我在这里的网站上看了很多东西,但无法弄清楚。 I have tried sth like this: 我已经尝试过……
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{....}
and then in the brackets repeat everything from DateType2 that I want to keep and add DatType1.fp, DatType1.fi, DatType1.fd. 然后在括号中重复我要保留的DateType2中的所有内容,并添加DatType1.fp,DatType1.fi,DatType1.fd。
If I did this with brute force I would do a double for loop and go through each line of DatType1 and see where I match a row in DatType2 and then add DatType1.fp, DatType1.fi, DatType1.fd - But this would be very slow 如果我用蛮力做到这一点,我会做一个for循环,遍历DatType1的每一行,看看我在DatType2中与一行匹配的位置,然后添加DatType1.fp,DatType1.fi,DatType1.fd-但这会非常慢
This however didnt work and is far from elegant! 但是,这没有用,而且远远不够优雅! ...:) Any pointers would be much appreciated. ... :)任何指针将不胜感激。
Thanks 谢谢
I would create a base class, with all the common data: 我将创建一个具有所有常见数据的基类:
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
}
}
And the others would inherit this base: 其他人将继承这个基础:
public class DatType1 : BaseClass
{
public string mac;
public string fuel;
}
public class DatType2 : BaseClass
{
public double pp;
public double pi;
}
Now you can use the a.HasSameSignature(b)
instead of a.yr==b.yr & a.qr==b.qr & a.RG == b.RG & a.tar ==b.tar
. 现在您可以使用a.HasSameSignature(b)
代替a.yr==b.yr & a.qr==b.qr & a.RG == b.RG & a.tar ==b.tar
。 And Set the values (or create a copy method, as you wish).... 并设置值(或根据需要创建复制方法)。
I would recommend overloading the equality comparison of the two objects to make the DatType1:IEquatable<DatType2>
and/or DatType2:IEquatable<DatType1>
. 我建议重载两个对象的相等比较,以使DatType1:IEquatable<DatType2>
和/或DatType2:IEquatable<DatType1>
。 That would allow you to use dat1.Equals(dat2)
or dat2.Equals(dat1)
. 那将允许您使用dat1.Equals(dat2)
或dat2.Equals(dat1)
。
Alternatively, you can implement a comparison delegate and pass that in as dat1.Equals(dat2,comparer)
or comparer.Compare(dat1,dat2)
. 或者,您可以实现一个比较委托并将其作为dat1.Equals(dat2,comparer)
或comparer.Compare(dat1,dat2)
。 The later is probably ideal if you don't have control over the DatType1
or DatType2
types. 如果您无法控制DatType1
或DatType2
类型,则DatType1
可能是理想的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.