I'm on a interop scenario, and because of that I'm dealing with structures and classes used like structures in different assemblies - so a cast is not enough and have to do manually field-by-field which is very boring and error prone.
我正处于一个互操作场景,因为我正在处理结构和类,就像在不同的程序集中使用结构一样 - 所以一个强制转换是不够的,并且必须手动逐字段进行,这非常无聊且容易出错。
So I devised an function that does copy the great deal of simple fields/properties and I deal only with the troubled ones.
所以我设计了一个复制大量简单字段/属性的函数,我只处理有问题的字段/属性。
When I do this to properties only, it works ok. But I need now how to fix this LiNQ
query to be able to get a list of fields from the source object and join them with the properties on the target object.
当我只对属性执行此操作时,它可以正常工作。但我现在需要如何修复此LiNQ查询以便能够从源对象获取字段列表并将它们与目标对象上的属性连接起来。
Code below:
代码如下:
var TypeOrig = pSource.GetType();
var TypeDest = pTarget.GetType();
var TypeString = typeof(System.String);
var PropOrig = TipoOrig.GetFields(); // if it is GetProperties instead
// of GetFields works OK
var PropDest = TipoDest.GetProperties();
var QryPropVT =
from
POrig in PropOrig
join PDest in PropDest
on new
{
POrig.Name,
POrig.FieldType
} equals new
{
PDest.Name,
PDest.PropertyType
}
where POrig.PropertyType.IsValueType || (POrig.PropertyType.Equals(TipoString))
select new
{
PropO = POrig,
PropD = PDest
};
Visual C# error: Error 2 The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
Visual C#错误:错误2 join子句中某个表达式的类型不正确。调用“加入”时类型推断失败。
EDIT: I saw value injector, but it's like using a Death Star to kill a mosquito...[/EDIT]
编辑:我看到价值注入器,但它就像使用死星杀死蚊子... [/编辑]
2 个解决方案
#1
3
Your join statement appears to be creating 2 different anonymous types since one has a property called FieldType
and one has a property called PropertyType
. LINQ is unable to do the join unless both types have the exact same fields in the exact same order. There is a wonderful article on this found here.
您的连接语句似乎创建了两种不同的匿名类型,因为其中一个具有名为FieldType的属性,另一个具有名为PropertyType的属性。除非两个类型具有完全相同的完全相同的字段,否则LINQ无法进行连接。这里有一篇很棒的文章。
In which case you'll need to do this for your join:
在这种情况下,您需要为您的加入执行此操作:
join PDest in PropDest
on new
{
Name = POrig.Name,
JoinType = POrig.FieldType
} equals new
{
Name = PDest.Name,
JoinType = PDest.PropertyType
}
#2
1
I think you might be after AutoMapper. http://automapper.codeplex.com/ or Value Injector http://valueinjecter.codeplex.com/
我想你可能会在AutoMapper之后。 http://automapper.codeplex.com/或Value Injector http://valueinjecter.codeplex.com/
Value Injector example:
值注入器示例:
myObject.InjectFrom(anyOtherObject);
//inject from multiple sources
a.InjectFrom(b,c,d,e);
//inject using your own injection
a.InjectFrom<MyInjection>(b);
#1
3
Your join statement appears to be creating 2 different anonymous types since one has a property called FieldType
and one has a property called PropertyType
. LINQ is unable to do the join unless both types have the exact same fields in the exact same order. There is a wonderful article on this found here.
您的连接语句似乎创建了两种不同的匿名类型,因为其中一个具有名为FieldType的属性,另一个具有名为PropertyType的属性。除非两个类型具有完全相同的完全相同的字段,否则LINQ无法进行连接。这里有一篇很棒的文章。
In which case you'll need to do this for your join:
在这种情况下,您需要为您的加入执行此操作:
join PDest in PropDest
on new
{
Name = POrig.Name,
JoinType = POrig.FieldType
} equals new
{
Name = PDest.Name,
JoinType = PDest.PropertyType
}
#2
1
I think you might be after AutoMapper. http://automapper.codeplex.com/ or Value Injector http://valueinjecter.codeplex.com/
我想你可能会在AutoMapper之后。 http://automapper.codeplex.com/或Value Injector http://valueinjecter.codeplex.com/
Value Injector example:
值注入器示例:
myObject.InjectFrom(anyOtherObject);
//inject from multiple sources
a.InjectFrom(b,c,d,e);
//inject using your own injection
a.InjectFrom<MyInjection>(b);