C# 将对象转换为列表
我想将对象转换为myclass的列表,该对象是从linq查询返回的C# 将对象转换为列表,c#,linq,list,object,C#,Linq,List,Object,我想将对象转换为myclass的列表,该对象是从linq查询返回的 object list = detailManager.GetMutabakatDetailListByMutabakat(oMutabakat, true); List<CurrentAccount> accountList = ?? 我能说的话很少 您的方法GetMutabakatDetailListByMutabakat返回什么?它必须是对象吗 顾名思义-如果方法名是Get…List,我希望它返回一个列表,而
object list = detailManager.GetMutabakatDetailListByMutabakat(oMutabakat, true);
List<CurrentAccount> accountList = ??
我能说的话很少 您的方法
GetMutabakatDetailListByMutabakat
返回什么?它必须是对象吗
顾名思义-如果方法名是Get…List
,我希望它返回一个列表,而不是一个对象。如果可以的话,首先改变这种行为,然后你就没事了
如果该方法是由其他人编写的(您无法更改该行为),那么您应该按照其他人的建议使用强制转换操作,但只有在底层对象的类型确实为List
时,您才能这样做
如果底层对象甚至不是List
类型,那么您应该了解对象结构是什么(另一个类、匿名对象或动态对象),然后我们可以解决一些问题
更新后
随着您问题的更新,我可以看到您正在返回一个匿名对象列表。但它实际上不是活期账户吗
因此,如果您选择以下选项:
public List<CariHesapEkstre> GetMutabakatDetailListByMutabakat ( Mutabakat mutabakat, bool Gonderen )
{
var detayIdList = this.Context.MutabakatDetay.Where(s => s.MutabakatId == mutabakat.MutabakatId).Select(s => s.MutabakatDetayId).ToList();
var CariEkstreList =
( from ekstre in this.Context.CariHesapEkstre
join detay in this.Context.MutabakatDetay on ekstre.MutabakatDetayId equals detay.MutabakatDetayId
where detayIdList.Contains(ekstre.MutabakatDetayId.Value) && ekstre.GonderenMukellefFirmaId == mutabakat.GonderenMukellefFirmaId
/// here you only need to use object initializer for CariHesapEkstre like just below
select new CariHesapEkstre
{
MutabakatDetayId = ekstre.MutabakatDetayId,
MutabakatVar = ekstre.MutabakatVar,
...
Tutar = ekstre.BakiyeTur == "B" ? ekstre.Tutar * -1 : ekstre.Tutar
} ).ToList();
return CariEkstreList;
}
public List getmutabakattaillistbymutabakat(Mutabakat Mutabakat,bool Gonderen)
{
var detayIdList=this.Context.MutabakatDetay.Where(s=>s.MutabakatId==mutabakat.MutabakatId).选择(s=>s.MutabakatDetayId).ToList();
变量CariEkstreList=
(来自ekstre in this.Context.CariHesapEkstre
在ekstre上的this.Context.MutabakatDetay中加入detay。MutabakatDetayId等于detay.MutabakatDetayId
其中detayIdList.Contains(ekstre.mutabakatetayid.Value)和&ekstre.GonderenMukellefFirmaId==mutabakat.GonderenMukellefFirmaId
///在这里,您只需要使用CariHesapEkstre的对象初始值设定项,如下所示
选择新CariHesapEkstre
{
mutabakatetayid=ekstre.mutabakatetayid,
MutabakatVar=ekstre.MutabakatVar,
...
Tutar=ekstre.BakiyeTur==“B”?ekstre.Tutar*-1:ekstre.Tutar
})。ToList();
返回CariEkstreList;
}
上次更新
我知道你想做什么,你想要一个方法来为你做这些努力。所以你可以检查这个叫做。它做你想做的事。但对于代码来说,这也是一项艰巨的工作
但是,如果您只是尝试将对象转换为列表,则可以使用下面的代码
public List<CariHesapEkstre> ConvertToDesiredType ( object list )
{
return ( (IEnumerable<dynamic>)list ).Select(item => new CariHesapEkstre
{
MutabakatDetayId = item.MutabakatDetayId,
MutabakatVar = item.MutabakatVar,
...
}).ToList();
}
public List ConvertToDesiredType(对象列表)
{
返回((IEnumerable)列表)。选择(项=>new CariHesapEkstre
{
mutabakatetayid=item.mutabakatetayid,
MutabakatVar=item.MutabakatVar,
...
}).ToList();
}
这取决于列表实际上是什么:
A) 如果detailManager.getmutabakattaillistbymutabakat(oMutabakat,true)
返回IEnumerable
,那么您所要做的就是添加.ToList()
:
C) 最后,在一般情况下,您必须实现。选择:
List<CurrentAccount> accountList = detailManager
.GetMutabakatDetailListByMutabakat(oMutabakat, true)
.Select(item => GetAccountFromItem(item)) //TODO: implement Select
.ToList();
List accountList=detailManager
.GetMutabakatDetailListByMutabakat(oMutabakat,true)
.Select(项=>GetAccountFromItem(项))//TODO:实现选择
.ToList();
谢谢大家,我用reflaction解决了我的问题。
首先,将对象强制转换为IList
List<CariHesapEkstre> senderExtractList = GetExtractList((IList)detayManager.GetMutabakatDetayListByMutabakat(oMutabakat, true));
private List<CariHesapEkstre> GetExtractList ( IList tempList )
{
List<CariHesapEkstre> returnList = new List<CariHesapEkstre>();
foreach ( var item in tempList )
{
CariHesapEkstre extract = new CariHesapEkstre();
foreach ( PropertyInfo prop in item.GetType().GetProperties() )
{
foreach ( PropertyInfo prop2 in extract.GetType().GetProperties() )
{
if ( prop2.Name == prop.Name )
{
prop2.SetValue(extract, prop.GetValue(item));
}
}
}
returnList.Add(extract);
}
return returnList;
}
List senderExtractList=GetExtractList((IList)detayManager.getmutabakatedaylistbymutabakat(oMutabakat,true));
私有列表GetExtractList(IList模板列表)
{
List returnList=新列表();
foreach(模板列表中的变量项)
{
CariHesapEkstre提取物=新的CariHesapEkstre();
foreach(item.GetType().GetProperties()中的PropertyInfo属性)
{
foreach(extract.GetType().GetProperties()中的PropertyInfo prop2)
{
if(prop2.Name==prop.Name)
{
prop2.SetValue(摘录,prop.GetValue(项目));
}
}
}
返回列表。添加(摘录);
}
退货清单;
}
.ToList()
如果GetMutabakatDetailListByMutabakat
没有返回IEnumerable
您必须添加Select
即。选择(项=>GetAccountFromItem(项)).ToList()代码>如何声明GetMutabakatDetailListByMutabakat
?是否有任何理由将其结果分配给对象
变量?该对象是否为CurrentAccount列表?如果是这样,则直接转换ittryList accountList=(List)List代码>是,我有理由返回将其结果分配给对象。对象在我尝试这样的cast时,返回了一个错误。我很高兴您提出了自己的解决方案。但我觉得有义务说,你最好不要这样做。反射是一个重量级的操作,您可以将它与三重for循环一起使用。即使这是可行的,也不意味着这是正确的方法。你可以用挖掘机或汽车从A点到B点。哪一个更有意义?那么,你的建议是什么?你可以检查我提供的答案。我也更新了代码。好的,谢谢:)但它对我的案子不起作用。我知道,方法返回的是列表而不是对象。但它是通用的。我的问题是对象如何转换为类的列表。就这些。
List<CurrentAccount> accountList = detailManager
.GetMutabakatDetailListByMutabakat(oMutabakat, true)
.OfType<CurrentAccount>()
.ToList();
List<CurrentAccount> accountList = detailManager
.GetMutabakatDetailListByMutabakat(oMutabakat, true)
.Select(item => GetAccountFromItem(item)) //TODO: implement Select
.ToList();
List<CariHesapEkstre> senderExtractList = GetExtractList((IList)detayManager.GetMutabakatDetayListByMutabakat(oMutabakat, true));
private List<CariHesapEkstre> GetExtractList ( IList tempList )
{
List<CariHesapEkstre> returnList = new List<CariHesapEkstre>();
foreach ( var item in tempList )
{
CariHesapEkstre extract = new CariHesapEkstre();
foreach ( PropertyInfo prop in item.GetType().GetProperties() )
{
foreach ( PropertyInfo prop2 in extract.GetType().GetProperties() )
{
if ( prop2.Name == prop.Name )
{
prop2.SetValue(extract, prop.GetValue(item));
}
}
}
returnList.Add(extract);
}
return returnList;
}