Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用linq将对象模型列表映射到另一个列表_C#_Linq - Fatal编程技术网

C# 使用linq将对象模型列表映射到另一个列表

C# 使用linq将对象模型列表映射到另一个列表,c#,linq,C#,Linq,我有两个对象模型,它们共享一些属性和一种类型的列表,我想用它们创建另一种类型的列表。现在,我有这样的东西(它是有效的): List TheListOfObjectsA=某个查询的结果; List TheListOfObjectsB=新列表(); foreach(列表对象中的对象) { ObjectB TheObjectB=新的ObjectB(); 对象B.Prop1=对象A.Prop1; TheObjectB.Prop2=TheObjectA.Prop2; 列表对象b.添加(对象b); } 我

我有两个对象模型,它们共享一些属性和一种类型的列表,我想用它们创建另一种类型的列表。现在,我有这样的东西(它是有效的):

List TheListOfObjectsA=某个查询的结果;
List TheListOfObjectsB=新列表();
foreach(列表对象中的对象)
{
ObjectB TheObjectB=新的ObjectB();
对象B.Prop1=对象A.Prop1;
TheObjectB.Prop2=TheObjectA.Prop2;
列表对象b.添加(对象b);
}
我很好奇是否以及如何在没有循环的情况下在linq语句中重写它(即使我们知道linq语句将作为循环执行)

谢谢。

列出ListFobjectsB=ListFobjectsA
List<ObjectB> TheListOfObjectsB = TheListOfObjectsA
    .Select(t => new ObjectB {
        Prop1 = t.Prop1,
        Prop2 = t.Prop2,
    }).ToList();
.Select(t=>newobjectb{ Prop1=t.Prop1, Prop2=t.Prop2, }).ToList();
您必须调用
ToList()
方法才能获得
列表。否则,您将得到一个
IEnumerable

你可以用这个

var theListOfObjectsB = theListOfObjectsA
    .Select(t => new ObjectB{
        Prop1 = t.Prop1,
        Prop2 = t.Prop2,
    }).ToList();
你是说这个吗

var TheListOfObjectsB  = TheListObjectsA.Select(a => new ObjectB() { Prop1  = a.Prop1, Prop2 = a.Prop2 }).ToList();
您可以使用:

这样做,并在公共场所定义converter方法,意味着您可以随时进行此转换,而无需复制逻辑

为了更简洁,您甚至可以在某个地方静态声明转换器:

private static Converter<ObjectA, ObjectB> _converter =
    new Converter<ObjectA, ObjectB>(ObjectAConverter);
public static Converter<ObjectA, ObjectB> ObjectAConverter { get { return _converter; } }
这应该有助于

var theListOfObjectsB  =  (from e in theListOfObjectsA  select new ObjectB{prop1 = e.prop1 ,prop2= e.prop2 }).ToList();

@Javad_Amiry:因为上下文中不存在对象A
。您没有使用
t
,那么对象B到底是什么?好的,简单明了!顺便说一句,您应该在结尾处编辑并添加.ToList()
public static ObjectB ObjectAConverter(ObjectA a)
{
    var b = new ObjectB();

    b.Prop1 = a.Prop1;
    b.Prop2 = a.Prop2;

    return b;
}
private static Converter<ObjectA, ObjectB> _converter =
    new Converter<ObjectA, ObjectB>(ObjectAConverter);
public static Converter<ObjectA, ObjectB> ObjectAConverter { get { return _converter; } }
var output = listA.ConvertAll(TypeName.ObjectAConverter);
var theListOfObjectsB  =  (from e in theListOfObjectsA  select new ObjectB{prop1 = e.prop1 ,prop2= e.prop2 }).ToList();