How to access a model through List in MVC - c#

I want to sort the ValidatingCarrier in ascending order so I thought of getting it into a List and sorting it using Orderby in my controller.
I have the following Model Classes:
public class sort
{
public OTAAirLowFareSearchRS OTA_AirLowFareSearchRS { get; set; }
}
public class OTAAirLowFareSearchRS
{
public int PricedItinCount { get; set; }
public int BrandedOneWayItinCount { get; set; }
public int SimpleOneWayItinCount { get; set; }
public int DepartedItinCount { get; set; }
public int SoldOutItinCount { get; set; }
public int AvailableItinCount { get; set; }
public string Version { get; set; }
public Success success { get; set; }
public Warnings Warnings { get; set; }
public PricedItineraries PricedItineraries { get; set; }
public TPAExtensions8 TPA_Extensions { get; set; }
}
public class PricedItinerary
{
public int SequenceNumber { get; set; }
public AirItinerary AirItinerary { get; set; }
public List<AirItineraryPricingInfo> AirItineraryPricingInfo { get; set; }
public TicketingInfo TicketingInfo { get; set; }
public TPAExtensions7 TPA_Extensions { get; set; }
}
public class AirItineraryPricingInfo
{
public string PricingSource { get; set; }
public string PricingSubSource { get; set; }
public ItinTotalFare ItinTotalFare { get; set; }
public PTCFareBreakdowns PTC_FareBreakdowns { get; set; }
public FareInfos2 FareInfos { get; set; }
public TPAExtensions6 TPA_Extensions { get; set; }
public string LastTicketDate { get; set; }
}
public class TPAExtensions6
{
public DivideInParty DivideInParty { get; set; }
public ValidatingCarrier ValidatingCarrier { get; set; }
}
The following is the way I am trying to access it in my controller. I cannot access beyond PriceItinerary cause the reset goes as a List.
sort searchResponse = JsonConvert.DeserializeObject<sort>(tar.ToString());
var sortingname = searchResponse.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary;
List<PricedItinerary> lstname = new List<PricedItinerary>();
Any kind of help is appreciated....

You have create the object first
searchResponse.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary obj = new searchResponse.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary;
var x = obj.SequenceNumber;

Are you trying to access "AirItineraryPricingInfo" with the instance of "PricedItinerary"? If it is so, then this helps, otherwise please ignore.
public class PricedItinerary : AirItineraryPricingInfo
{
public int SequenceNumber { get; set; }
public AirItinerary AirItinerary { get; set; }
public List<AirItineraryPricingInfo> AirItineraryPricingInfo { get; set; }
public TicketingInfo TicketingInfo { get; set; }
public TPAExtensions7 TPA_Extensions { get; set; }
}
In controller if we create instance for "PricedItinerary", we can access inherited class.

You can do it with LINQ SelectMany makes a projection os IEnumerable into one IEnumerable.
With the given model and assuming that you need to get a list of ValidatingCarrier starting on PricedItinerary , let's go do it!
IEnumerable<ValidatingCarrier> IEValidatingCarrier = searchResponse.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary
.SelectMany(p => p.AirItineraryPricingInfo.SelectMany(a => a.TPA_Extensions.ValidatingCarrier)); //This would give you an IEnumerable<ValidatingCarrier>
IEnumerable<ValidatingCarrier> LstValidatingCarrier = IEValidatingCarrier.OrderBy(o => o.OrderBySomething).ToList(); //And finally, your ordered list!
You can also do it all at once, something like this:
searchResponse.OTA_AirLowFareSearchRS.PricedItineraries.PricedItinerary
.SelectMany(p => p.AirItineraryPricingInfo.SelectMany(a => a.TPA_Extensions.ValidatingCarrier))
.OrderBy(o => o.OrderBySomething).ToList();
Hope it helps! Plase if I don't understand well your question, please let me know!

Related

Reuse DTO with different child/nested object

Imagine I have two DTOs that share top level types (ServerResponseDTO, ServerCallDetails) but the Items object has different child object (ItemsOfTypeA vs ItemsOfTypeB). What would be the best way to reuse defined top level classes without code duplication? - how can I easily instantiate next objects for ItemsOfTypeC, D and so on.
DTO 1:
public class ServerResponseDTO
{
public int CallId { get; set; }
public ServerCallDetails Details { get; set; }
}
public class ServerCallDetails
{
public int Id { get; set; }
public Items Items { get; set; }
}
public class Items
{
public int Id { get; set; }
public ItemsOfTypeA Items { get; set; }
}
DTO 2:
public class ServerResponseDTO
{
public int CallId { get; set; }
public ServerCallDetails Details { get; set; }
}
public class ServerCallDetails
{
public int Id { get; set; }
public Items Items { get; set; }
}
public class Items
{
public int Id { get; set; }
public ItemsOfTypeB Items { get; set; }
}
but is there a way without passing T to very bottom object?
No. Imagine you could write the following:
public class Items
{
public int Id { get; set; }
public T Items<T> { get; set; }
}
What would be the type of the foo variable in the following code?
var items = new Items();
var foo = pair.Items;
So you have to declare type where your Items<T> is used:
public class ServerResponseDTO<T>
{
public int CallId { get; set; }
public ServerCallDetails<T> Details { get; set; }
}
public class ServerCallDetails<T>
{
public int Id { get; set; }
public Items<T> Items { get; set; }
}
public class Items<T>
{
public int Id { get; set; }
public T FooBar { get; set; }
}
I.e. one of the solution might be to use generic type as Items property so now I can easily create new DTO like: ServerResponseDTO<ItemOfTypeC>
public class ServerResponseDTO<T>
{
public int CallId { get; set; }
public ServerCallDetails<T> Details { get; set; }
}
public class ServerCallDetails<T>
{
public int Id { get; set; }
public Items<T> Items { get; set; }
}
public class Items<T>
{
public int Id { get; set; }
public T Items { get; set; }
}

DynamoDB how to use ScanAsync on a nested structure?

Anyone have any idea / example how can I get the list of DynamoDbRepo with all nested structure if in DynamoDbMethodParameter.Name == specific value?
I suppose I should use ScanAsync but with what setting( ScanRequest ) ?
[DynamoDBTable("REPO_TABLE")]
public class DynamoDbRepo
{
[DynamoDBProperty("Id")]
[DynamoDBHashKey]
public int Id { get; set; }
[DynamoDBProperty("Solutions")]
public List<DynamoDbSolution> Solutions { get; set; }
}
public class DynamoDbSolution
{
[DynamoDBProperty("Path")]
public string Path { get; set; }
[DynamoDBProperty("Methods")]
public List<DynamoDbMethod> Methods { get; set; }
}
public class DynamoDbMethod
{
[DynamoDBProperty("Name")]
public string Name { get; set; }
[DynamoDBProperty("MethodParameters")]
public List<DynamoDbMethodParameter> MethodParameters { get; set; }
}
public class DynamoDbMethodParameter
{
[DynamoDBProperty("Type")]
public string Type { get; set; }
[DynamoDBProperty("Name")]
public string Name { get; set; }
}

Automapper missing type map configuration

I can't seem to figure out what is going wrong here, I have configured AutoMapper as follows
services.AddAutoMapper(typeof(MetingenView), typeof(Meting));
And in the controller like this:
public MetingenController(IMapper mapper)
{
this._mapper = mapper;
}
After, I use it like this:
var entity = await this.Context.MetingenView.AsNoTracking().FirstOrDefaultAsync(g =>g.IdMeting == key);
if (entity == null)
{
return NotFound();
}
data.Patch(entity);
var meting = await this.Context.Meting.FirstOrDefaultAsync(m => m.IdMeting == key);
this._mapper.Map(entity, meting);
Then the error rolls out:
AutoMapper.AutoMapperMappingException: Missing type map configuration
or unsupported mapping.
EDIT:
Here are the Meting, and MetingenView classes:
public partial class Meting
{
public int IdMeting { get; set; }
public int IdKoeling { get; set; }
public int IdWerknemer { get; set; }
public int IdGebouw { get; set; }
public int Temperatuur { get; set; }
public DateTime AfgenomenTijd { get; set; }
public string ProductNaam { get; set; }
public string Actie { get; set; }
public DateTime? DatumOntstaan { get; set; }
public DateTime? DatumMutatie { get; set; }
public int IndVerwijderd { get; set; }
public DateTime? DatumVerwijderd { get; set; }
public virtual Gebouw IdGebouwNavigation { get; set; }
public virtual Koeling IdKoelingNavigation { get; set; }
public virtual Werknemer IdWerknemerNavigation { get; set; }
}
public partial class MetingenView
{
[Key]
public int IdKlant { get; set; }
public string Locatie { get; set; }
public string SoortKoeling { get; set; }
public int IdMeting { get; set; }
public int IdKoeling { get; set; }
public int IdWerknemer { get; set; }
public int IdGebouw { get; set; }
public int Temperatuur { get; set; }
public string Actie { get; set; }
public string ProductNaam { get; set; }
public DateTime AfgenomenTijd { get; set; }
}
I think the mapping between Meting and MetingenView is not configured in AutoMapper. If you use Asp.Net Core, you could create a profile.
public class MetingProfile : Profile
{
public MetingProfile()
{
CreateMap<MetingenView, Meting>();
}
}
This would create a default mapping that two types have the same property. If you want to config property mapping manually, Function ForMember() would be used.
For example, if you wish that the property MetingenView.IdGebouw maps Meting.IndVerwijderd, you can code this:
CreateMap<MetingenView, Meting>()
.ForMember(dest=>dest.IdGebouw, opt=>opt.MapFrom(src=>src.IndVerwijderd));

Nested Collection Mapping in Automapper

Am trying to map nested collections using automapper and I have done the basic setup and configuration. When I try to do the map it the nested values are coming as null. I have tried to follow few posts and put together something. I want the list to have a hierarchy instead of flattening. Any help around this would be great.
Source Entities:
public class OuterEntity
{
public int ID { get; set; }
public string Name { get; set; }
public List<InnerEntity> InnerEntityList { get; set; }
}
public class InnerEntity
{
public int InnerId { get; set; }
public string InnerName { get; set; }
public List<InnerMostEntity> InnerMostList { get; set; }
}
public class InnerMostEntity
{
public int InnerMostId { get; set; }
public string InnerMostName { get; set; }
public DateTime ModifiedDate { get; set; }
}
Destination Entities:
public class OuterEntityDTO
{
public int ID { get; set; }
public string Name { get; set; }
public List<InnerEntity> InnerEntityList { get; set; }
}
public class InnerEntityDTO
{
public int InnerId { get; set; }
public string InnerName { get; set; }
public List<InnerMostEntity> InnerMostList { get; set; }
}
public class InnerMostEntityDTO
{
public int InnerMostId { get; set; }
public string InnerMostName { get; set; }
public DateTime ModifiedDate { get; set; }
}
Controller Class:
public List<OuterEntityDTO> GetAll()
{
var outerEntityList = myRepo.GetAll(); //Type of List<OuterEntity>
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<OuterEntity, OuterEntityDTO>().ReverseMap();
cfg.CreateMap<InnerEntity, InnerEntityDTO>().ReverseMap();
cfg.CreateMap<InnerMostEntity, InnerMostEntityDTO>().ReveseMap();
});
config.AssertConfigurationIsValid();
var innerMostDTO = Mapper.Map<List<OuterEntity>,List<OuterEntityDTO>>(outerEntityList);
//The inner list at first level itself is null.
return innerMostDTO;
}
Am trying to achieve this in DOT NET Core. Autommaper version is 6.1.1
I think you should have a wrong class hierarchy in DTO classes, as you have
public List<InnerMostEntity> InnerMostList { get; set; }
in public class InnerEntityDTO, you should write it as
public List<InnerMostEntityDTO> InnerMostList { get; set; }

Remove a parent based on child values

I have the following data structure
public class Prediction
{
public string Description {get;set;}
public string Id { get; set; }
public List<MatchedSubstring> Matched_substrings { get; set; }
public string Place_id { get; set; }
public string Reference { get; set; }
public List<Term> Terms { get; set; }
public List<string> Types { get; set; }
}
public class GooglePlaceAutocompleteResult
{
public List<Prediction> Predictions { get; set; }
public string Status { get; set; }
}
What I would like to do is remove any items in the Predictions list if their Types collection contains the string "sublocality".
How can I do this with LINQ?
Assuming you are writing a method within GooglePlaceAutocompleteResult, you can write:
Predictions = Predictions.Where(p => !p.Types.Any(t => t.Contains("sublocality")).ToList();
Otherwise the code would be the same but would be result.Predictions = result.Predictions...

Categories

Resources