i have two classes as below i need to cast list<child> to list<BM>
public class Child
{
[JsonProperty("name")]
public string title { get; set; }
public string type { get; set; }
public string url { get; set; }
public List<Child> children { get; set; }
}
public class BM
{
public string title { get; set; }
public string type { get; set; }
public string url { get; set; }
public List<BM> children { get; set; }
}
You can't cast it. You can create new list with all items transformed from one class to another, but you can't cast the entire list at once.
You should probably create a method which will transform Child into BM instance:
public static BM ToBM(Child source)
{
return new BN() {
title = source.title,
type = source.type,
url = source.url,
children = source.children.Select(x => ToBM(x)).ToList()
};
}
and then use LINQ to transform entire list:
var BMs = source.Select(x => ToBM(x)).ToList();
use automapper's DLL. You can read more about automapper at http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays
List<BM> BMList=
Mapper.Map<List<Child>, List<BM>>(childList);
same question has been asked before
Automapper copy List to List
You can also use Custom Type Conversions if you want to cast between custom types like that:
Class2 class2Instance = (Class2)class1Instance;
So all you need is to define explicit or implicit conversion function in your child class.
// Add it into your Child class
public static explicit operator BM(Child obj)
{
BM output = new BM()
{
title = obj.title,
type = obj.type,
url = obj.url,
children = obj.children.Select(x => BM(x)).ToList()
};
return output;
}
and then:
var result = source.Select(x => (BM)x).ToList();
Related
I have just started using AutoMapper on an asp net core project and I'm trying to map an object that has a collection of an object that also has a collection of an object to an entity.
The source
public class MyClass
{
public List<MyCollection> MyCollections { get; set; }
}
public class MyCollection
{
public int CollectionId { get; set; }
public List<Description> Descriptions { get; set; }
}
public class Description
{
public int DescriptionId { get; set; }
public string Text { get; set; }
}
The destination
public class DescriptionToCollection
{
public int DescriptionId { get; set; }
public int CollectionId { get; set; }
}
I've played around with ConvertUsing thinking something like this, but I can't make it work.
CreateMap<MyClass, List<DescriptionToCollection>>()
.ConvertUsing(source => source.MyCollections.Select(x =>x.Description.Select(y=> new DescriptionToCollection{ DescriptionId=y.DescriptionId,CollectionId=x.CollectionId}).ToList()
));
Searching AutoMappers docs and the internet, I couldn't find anything similar to my problem.
Any help is highly appreciated.
Besides a typo in your example code, you almost had it. Because you aren't mapping 1:1 at the top level, you need to flatten somewhere, and you do that using SelectMany, moving the ToList call appropriately.
CreateMap<MyClass, List<DescriptionToCollection>>()
.ConvertUsing(source => source.MyCollections.SelectMany(x => // SelectMany to flatten
x.Descriptions.Select(y =>
new DescriptionToCollection
{
DescriptionId = y.DescriptionId,
CollectionId = x.CollectionId
}
) // ToList used to be here
).ToList()
);
Try to implement ITypeConverter, follow the example code:
Your Classes
public class Class1
{
public List<Class2> class2 { get; set; }
}
public class Class2
{
public int CollectionId { get; set; }
public List<Class3> class3 { get; set; }
}
public class Class3
{
public int DescriptionId { get; set; }
public string Text { get; set; }
}
public class ClassDto
{
public int DescriptionId { get; set; }
public int CollectionId { get; set; }
}
The custom map
public class ClassCustomMap : ITypeConverter<Class1, List<ClassDto>>
{
public List<ClassDto> Convert(Class1 source, List<ClassDto> destination, ResolutionContext context)
{
var classDtoList = new List<ClassDto>();
foreach (var item in source.class2)
{
var collectionID = item.CollectionId;
foreach (var obj in item.class3)
{
var classDto = new ClassDto();
classDto.CollectionId = collectionID;
classDto.DescriptionId = obj.DescriptionId;
classDtoList.Add(classDto);
}
}
return classDtoList;
}
}
The mapping declaration
CreateMap<Class1, List<ClassDto>>().ConvertUsing<ClassCustomMap>();
How to use it
var class2 = new Class2();
class2.CollectionId = 2;
var class3 = new Class3();
class3.DescriptionId = 1;
class3.Text = "test";
class2.class3 = new System.Collections.Generic.List<Class3>() { class3 };
var class1 = new Class1();
class1.class2 = new System.Collections.Generic.List<Class2>() { class2 };
var result = mapper.Map<List<ClassDto>>(class1);
For clarity and to simplify I have used explicit cycles, if you want you can optimize the conversion function using LinQ and Lambda
You are missing the purpose of auto-mapper.
It should be used for transforming an input object of one type into an output object of a different type.
And you wanted to create a map from MyClass type to List - this should be treated as projection.
You can achieve that using LINQ (for example as a extension method on MyClass):
public static class MyClassExtension
{
public static List<DescriptionToCollection> ToDescriptionToCollection(this MyClass value)
{
return value.MyCollections.SelectMany(mc => mc.Descriptions.Select(d => new DescriptionToCollection()
{
CollectionId = mc.CollectionId,
DescriptionId = d.DescriptionId
})).ToList();
}
}
Here is my code .Can be run in Linqpad.
void Main(){
List<Object> listobj=new List<object>{
new object[]{"A01001","sucess","on"},
new object[]{"A01002","fail","off"}};
listobj.Dump();
var second = listobj.Cast<RowItem>();
second.Dump();
}
public class RowItem {
public string GOODS_ID { get; set; }
public string AUDIT_STATUS { get; set; }
public string APPLY_STATUS { get; set; }
}
I want convert List<object> to List<RowItem>, but when I am using the cast method, it returns an error cannot convert type 'System.Object[]' to class RowItem
Is this possible, or is there another way of accomplishing this?
Thank you.
Your listobj's type is incorrect at many levels. The type is a collection of object arrays concealed as object array. If you want to continue with the same type, here is the code that will work.
void Main()
{
List<Object> listobj = new List<object>{
new object[]{"A01001","sucess","on"},
new object[]{"A01002","fail","off"}};
listobj.Dump();
var second = listobj.Cast<object[]>().Select(l => new RowItem {
GOODS_ID=l[0].ToString(),
AUDIT_STATUS=l[1].ToString(),
APPLY_STATUS=l[2].ToString()
});
second.Dump();
}
public class RowItem
{
public string GOODS_ID { get; set; }
public string AUDIT_STATUS { get; set; }
public string APPLY_STATUS { get; set; }
}
I am still learning how to use LINQ and now I'm struggling with this situation.
I have this XML file (example)
<Results>
<PrxComissao>
<Id>0</Id>
<NumErro>0</NumErro>
<RetCode>0</RetCode>
<IdEvento>0</IdEvento>
<ExecutionTimeMilliseconds>63596143450994.227</ExecutionTimeMilliseconds>
<ExecutionTimeSeconds>63596143450.994225</ExecutionTimeSeconds>
<CodComissao>CFE</CodComissao>
<Montante>20.00</Montante>
<Percentagem>0.0000</Percentagem>
<MntMin>0.00</MntMin>
<MntMax>0.00</MntMax>
<Nome>CFE</Nome>
<Descricao>Custo Factura Estrangeiro</Descricao>
</PrxComissao>
<PrxComissao>
<Id>0</Id>
<NumErro>0</NumErro>
<RetCode>0</RetCode>
<IdEvento>0</IdEvento>
<ExecutionTimeMilliseconds>63596143450994.227</ExecutionTimeMilliseconds>
<ExecutionTimeSeconds>63596143450.994225</ExecutionTimeSeconds>
<CodComissao>CFE</CodComissao>
<Montante>20.00</Montante>
<Percentagem>0.0000</Percentagem>
<MntMin>13.00</MntMin>
<MntMax>123.00</MntMax>
<Nome>CFE</Nome>
<Descricao>www</Descricao>
</PrxComissao>
</Results>
And now what I want to do is get all the XML elements inside the "PrxComissao", and then assign them to my class. This is the code I was trying
XDocument xDoc = XDocument.Parse(resultado);
List<PrxComissao> lstPrxComissao = xDoc.Elements("Results")
.Elements("PrxComissao")
.Elements()
.Select(BL_CartaoCredito.Utils.Data.Converter.FromXElement<PrxComissao>)
.ToList();
ObjAuxResult = lstPrxComissao;
What I am trying to do with this Converter.FromXElement<PrxComissao> is get all that elements and assign them.
Here is my class
public class PrxComissao
{
public string CodComissao { get; set; }
public string Montante { get; set; }
public string Percentagem { get; set; }
public string MntMin { get; set; }
public string MntMax { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public string TipoImposto { get; set; }
public string ComFinanciamento { get; set; }
public string iActivo { get; set; }
public string UtlModificacao { get; set; }
public string DtModificacao { get; set; }
public string UtlCriacao { get; set; }
public string DtCriacao { get; set; }
}
public static T FromXElement<T>(XElement element) where T : class, new()
{
T value = new T();
foreach (var subElement in element.Elements())
{
var field = typeof(T).GetField(subElement.Name.LocalName);
field.SetValue(value, (string)subElement);
}
return value;
}
So now I have two problems. First, I can't get to the elements inside PrxComissao always returns me nothing and then is my LINQ Select correct ? Or is there a better way ?
Start with the Descendants assuming your convertor takes an XElement:
List<PrxComissao> lstPrxComissao = xDoc.Descendants()
.Elements("PrxComissao")
.Select(el => BL_CartaoCredito.Utils.Data.Converter.FromXElement<PrxComissao>(el))
.ToList();
and then (untested) ...
public static T FromXElement<T>(XElement element) where T : class, new()
{
var typeOfT = typeof(T);
T value = new T();
foreach (var subElement in element.Elements())
{
var prop = typeOfT.GetProperty(subElement.Name.LocalName);
if(prop != null)
{
prop.SetValue(value, subElement.Value);
}
}
return value;
}
Currently, your code passes individual child elements of <PrxComissao> to the converter method. I believe you want to pass XElement that references <PrxComissao> instead :
List<PrxComissao> lstPrxComissao =
xDoc.Elements("Results")
.Elements("PrxComissao")
.Select(o => BL_CartaoCredito.Utils
.Data
.Converter
.FromXElement<PrxComissao>(o)
)
.ToList();
Besides, your class uses properties instead of field, so the corresponding reflection method supposed to be used here is GetProperty(), not GetField().
Here is my data transfer object
public class LoadSourceDetail
{
public string LoadSourceCode { get; set; }
public string LoadSourceDesc { get; set; }
public IEnumerable<ReportingEntityDetail> ReportingEntity { get; set; }
}
public class ReportingEntityDetail
{
public string ReportingEntityCode { get; set; }
public string ReportingEntityDesc { get; set; }
}
And here is my ViewModel
public class LoadSourceViewModel
{
#region Construction
public LoadSourceViewModel ()
{
}
public LoadSourceViewModel(LoadSourceDetail data)
{
if (data != null)
{
LoadSourceCode = data.LoadSourceCode;
LoadSourceDesc = data.LoadSourceDesc;
ReportingEntity = // <-- ? not sure how to do this
};
}
#endregion
public string LoadSourceCode { get; set; }
public string LoadSourceDesc { get; set; }
public IEnumerable<ReportingEntityViewModel> ReportingEntity { get; set; }
}
public class ReportingEntityViewModel
{
public string ReportingEntityCode { get; set; }
public string ReportingEntityDesc { get; set; }
}
}
I'm not sure how to transfer the data from the LoadSourceDetail ReportingEntity to the LoadSourceViewModel ReportingEntity. I'm trying to transfer data from one IEnumerable to another IEnumerable.
I would use AutoMapper to do this:
https://github.com/AutoMapper/AutoMapper
http://automapper.org/
You can easily map collections, see https://github.com/AutoMapper/AutoMapper/wiki/Lists-and-arrays
It would look something like this:
var viewLoadSources = Mapper.Map<IEnumerable<LoadSourceDetail>, IEnumerable<LoadSourceViewModel>>(loadSources);
If you are using this in an MVC project I usually have an AutoMapper config in the App_Start that sets the configuration i.e. fields that do not match etc.
Without AutoMapper you will have to map each property one by one ,
Something like this :
LoadSourceDetail obj = FillLoadSourceDetail ();// fill from source or somewhere
// check for null before
ReportingEntity = obj.ReportingEntity
.Select(x => new ReportingEntityViewModel()
{
ReportingEntityCode = x.ReportingEntityCode,
ReportingEntityDesc x.ReportingEntityDesc
})
.ToList(); // here is 'x' is of type ReportingEntityDetail
You could point it to the same IEnumerable:
ReportingEntity = data.ReportingEntity;
If you want to make a deep copy, you could use ToList(), or ToArray():
ReportingEntity = data.ReportingEntity.ToList();
That will materialize the IEnumerable and store a snapshot in your view model.
I need to add data to view model using LINQ
My view model is :
public class SearchScrapViewModel
{
public WClass wClass{get; set;}
public SClass sClass{get; set;}
public YClass yClass { get; set; }
}
public class WClass
{
public string title { get; set; }
public string link { get; set; }
}
public class SClass
{
public string title { get; set; }
public string link { get; set; }
}
public class YClass
{
public string title { get; set; }
public string link { get; set; }
}
and i need to use these 3 classes with 3 different LINQ query and then pass data to return View(SearchScrapViewModel);
var wikians = //LINQ Logic
select new SearchScrapViewModel
{
wClass.link = link.Attributes["href"].Value, //Error: I am not able to add to wClass
wClass.title = link.InnerText
};
and similarly to other classes
and then pass to return View(SearchScrapViewModel); so that i can access all the 3 classes in View of this controller
How to do that?
You forgot to create an instance of your WClass:
select new SearchScrapViewModel {
wClass = new WClass {
link = link.Attributes["href"].Value,
title = link.InnerText
}
};
Alternatively, you could make WClass (and SClass and YClass) a struct instead of a class, then you don't need to instantiate it. In that case, however, you should probably make the struct immutable.
LINQ is not the be-all-end-all, and I dont know that this is the best approach for what you are looking for. I would suggest looking at the Builder Pattern, to accomplish this. If you really want to, you could do this in one LINQ query (using object initializers), but it might not read as clean as a builder would (but that is my two cents):
select new SearchScrapViewModel
{
wClass = new wClass{title = xyz, link = xyz},
sClass = new sClass...
yClass = new yClass...
}
It is not clear to me why you need a select statement in your example. In any case you can't return SearchScrapViewModel as your return because that is a type and not the instance. Unless your code is simplified for this post and you do need linq, I would suggest:
var wikians =
new SearchScrapViewModel {
wClass = new WClass {
link = link.Attributes["href"].Value,
title = link.InnerText
}
};
return View(wikians);