Hi I want to merge two objects together so that i can use the merged object to do something else.it's actually a simple airport program, i have "Route" and ""Flight" and if i want to add getting ticket i need each flight to be on a route if you know what i mean.but notice that i don't want to compare them.and i have an array of objects for each of them if it helps.
IRoute R = new LocalRoute(port, destination, routenum);
IFlight F = new LocalFlight(time, date,capacity);
I would give the Flight objects a Route property. This makes more sense to me than creating a class to hold both a Flight and a Route, since in the real world a flight should know which route it has to take.
For example:
public class Route {
public int Number { get; private set; }
public string Port { get; private set; }
public string Destination { get; private set; }
public Route(int number, string port, string destination) {
Number = number;
Port = port;
Destination = destination;
}
}
public class Flight {
public DateTime DepartureTime { get; private set; }
public int Capacity { get; private set; }
public Route Route { get; private set; }
public Flight(DateTime departureTime, int capacity, Route route) {
DepartureTime = departureTime;
Capacity = capacity;
Route = route;
}
}
Then you can have a list of possible routes and a list of flights, each of which has a reference to a route.
Would this do what you are looking for?
public class FlightDetails
{
IRoute Route {get;set}
IFlight Flight {get;set;}
}
For a more clear picture you can do something like this
public class FlightDetails
{
IRoute Route {get;set}
IFlight Flight {get;set;}
public FlightDetails(IRoute r,IFlight f)
{
this.Route = r;this.Flight = f;
}
}
IRoute R = new LocalRoute(port, destination, routenum);
IFlight F = new LocalFlight(time, date,capacity);
FlightDetails Fd = new FlightDetails(R,F);
Related
I am currently struggling to accept a list of objects from FormData in ASP.NET Core.
The project looks like this:
I have a class called Stavka (English: Item).
public class Stavka
{
public string naziv { get; set; }
public double cenaPoJedinici { get; set; }
public string jedinicaMere { get; set; }
public int kolicina { get; set; }
public Stavka(string naziv, double cenaPoJedinici, string jedinicaMere, int kolicina)
{
this.naziv = naziv;
this.cenaPoJedinici = cenaPoJedinici;
this.jedinicaMere = jedinicaMere;
this.kolicina = kolicina;
}
public Stavka()
{
}
}
I have a class called Faktura (English: Bill) which has a variable called Stavke (English: Items) that is a list containing the Stavka objects.
public class Faktura
{
public int Id { get; set; }
public string pibStart { get; set; }
public string pibEnd { get; set; }
public DateTime datumGen { get; set; }
public DateTime datumRok { get; set; }
public List<Stavka> stavke { get; set;}
public double cena { get; set; }
public string tip { get; set; }
public Faktura(int id, string pibStart, string pibEnd, DateTime datumGen, DateTime datumRok, List<Stavka> stavke, string tip)
{
Id = id;
this.pibStart = pibStart;
this.pibEnd = pibEnd;
this.datumGen = datumGen;
this.datumRok = datumRok;
this.stavke = stavke;
this.tip = tip;
double sumCena = 0;
foreach(Stavka s in stavke)
{
sumCena += s.kolicina * s.cenaPoJedinici;
}
this.cena = sumCena;
}
public Faktura()
{
}
I want to create a new Faktura object and add it to a list within my Controller. I tried to do this with the following code:
[HttpPost("dodajFakturu")]
public IActionResult dodajFakturu([FromForm]string pibStart, [FromForm]string pibEnd,[FromForm]DateTime datumStart, [FromForm]DateTime datumEnd,[FromForm]List<Stavka> stavkeLis, [FromForm]string tip)
{
int id = lst.OrderByDescending(p => p.Id).First().Id + 1;
Faktura f = new Faktura(id, pibStart,pibEnd, datumStart,datumEnd,stavkeLis,tip);
lst.Add(f);
return Ok(SveFakture());
}
And yet, when i post the request (in Swagger/Postman), the variable stavkeLis (which accepts the JSON array) is always empty:
This is certainly because i fundamentally misunderstood the way in which NET Core accepts these variables.
Is there some other way to send a list of objects through form data?
this way you have is currect, but if its not maybe because simple code problem but way that you right the code can be better or you can say develop your code as Below:
// StavkaBody => I Mean All Body In One Json
public async Task<IActionResult> MethodName([FromForm] string
StavkaBody)
{
YourObjectType object = new YourObjectType();
// this will be Populate All Json To Single object And
// You dont Need To Add some Constructors For Done this
JsonConvert.PopulateObject(StavkaBody, objec);
// Example Usage
Console.WriteLine(object.Name);
}
in Here I`ve Used The Newtonsoft.Json For this And Its Make Your Model So Much Simpler.
I Hope Its Helps
I have three classes:
public class M2ArticleMain
{
public int Id { get; set; }
public List<M2ArticleAttributeWeb> Attribut_Web { get; set; }
}
public class M2ArticleAttributeWeb
{
public int Web_Id { get; set; }
public M2ArticleTmpMainSkus Variants { get; set; }
}
public class M2ArticleTmpMainSkus
{
public DateTime TimeAdded { get; set; }
public List<string> Skus { get; set; }
}
And I have two Lists in my code like this:
List<M2ArticleMain> data = new List<M2ArticleMain>();
List<M2ArticleAttributeWeb> attb = new List<M2ArticleAttributeWeb>();
In some part of my code firstly I (from foreach loop) add data to attb list where I add only only some data (because I don't have all data at this point), like this:
...
attb.Add(new M2ArticleAttributeWeb
{
Web_id = item.Id, //(item is from foreach loop)
Variants = null //this is **importat**, I left null for later to add it
});
Next, after I fill attb, I add all this to data list:
...
data.Add(new M2ArticleMain
{
Id = item.Id_Pk, //this is also from foreach loop,
Attribut_Web = attb //now in this part I have only data for Web_id and not Variants
}
Now my question is How to Add items later to data list to object Variants?
Something like this:
data.AddRange( "how to point to Variants" = some data);
The M2ArticleAttributeWeb type holding your Variants property is the member of a collection. That is, there are potentially many of them. You can reference an individual Variants property like this:
data[0].Attribut_Web[0].Variants
But you need to know which items you want to add map to which data and Attribut_Web indexes/objects in order to assign them properly. That probably means another loop, or even a nested loop. That is, you can see all of your Variants properties in a loop like this:
foreach(var main in data)
{
foreach(var attrw in main)
{
var v = attrw.Variants;
// do something with v
Console.WriteLine(v);
// **OR**
attrw.Variants = // assign some object
}
}
It's also much better practice to create your collection properties with the object, and then give them private set attributes:
public class M2ArticleMain
{
public int Id { get; set; }
public List<M2ArticleAttributeWeb> Attribut_Web { get; private set; } = new List<M2ArticleAttributeWeb>();
}
public class M2ArticleAttributeWeb
{
public int Web_Id { get; set; }
public M2ArticleTmpMainSkus Variants { get; set; }
}
public class M2ArticleTmpMainSkus
{
public DateTime TimeAdded { get; set; }
public List<string> Skus { get; private set; } = new List<string>();
}
Now instead of assigning Attribut_Web = attb, you would need to .Add() to the existing List.
I have a list of elements that contain another list.
public class SiloNode
{
public string Key { get; private set; }
public string Url { get; private set; }
public List<NodeQuery> Queries { get; private set; }
}
public class NodeQuery
{
public string Query { get; private set; }
public int Seq { get; private set; }
}
I need to produce a new list containing all queries, with the corresponding parent url.
An example would be:
https://www.bbc.co.uk, How old is the BBC?
https://www.bbc.co.uk, Where can I find a guide to BBC channels?
https://www.bbc.co.uk, How is the BBC funded?
https://www.channel4.com, More queries about channel 4 ...
The output should be in the form of class LinkMeta.
public class LinkMeta
{
public LinkMeta(string url, string text)
{
Url = url;
Text = text;
}
public string Text { get; private set; }
public string Url { get; private set; }
}
The complete list is contained in: root.Children.
The query below gives me all Query elements but I can't get back to Url.
var filtered = root.Children.SelectMany(x => x.Queries.Select(y => y.Query));
You can achieve it with the below linq.
var filtered = (from snode in root
from qry in snode.Queries
select new LinkMeta(snode.Url, qry.Query)).ToList();
It will return List of LinkMeta. Hope it helps.
Lambda expressions have the same scope as the nested block. This means that the nested lambda expression still has access to the variable 'x'. So, you should be able to do the following:
var filtered = root.SelectMany(x => x.Children.Queries.Select(y => new LinkMeta(x.Url, y.Query));
I need a clear example that shows me how to define a list that has n rows and 4 columns and how to use it. I need a list to save my data like the below image. as you see this could be a dictionary.
You need to create a class with all the above properties
public class Sample
{
public string vocabulary { get; set; }
public string meaning { get; set; }
public int number { get; set; }
public int group { get; set; }
}
and then you can create a List of type Sample,
List<Sample> yourList = new List<Sample>();
You can add items to the list as below
yourList.Add(new Sample { vocabulary = "massive", meaning = "very big", number = 5, group = 15 });
You can access them later like this, if you want the first element,
var result = yourList[0];
this is the easiest and best way of doing it. You need to create a new class and then create new instances of the class and then add it to the list and then use LINQ to get the data out
void Main()
{
var list = new List<myClass>()
list.Add(new myClass() {
Vocabluary = "Vocabluary ",
Meaning = "meaning",
Number = 1,
Group = 2})
}
public class myClass
{
public string Vocabluary { get; set; }
public string Meaning { get; set; }
public int Number { get; set; }
public int Group { get; set; }
}
yes... as Sajeetharan mentioned, with a custom class you can create an any dimensions List. but i don't think you need to think about dimension in C#... it is a bit more high level than that.
just simply create a class and put everything you need in it...
public class CustomClass{
public string d1;
public int d2;
public string d3;
public string d4;
...
//you can easily create a N dimension class
}
to access it and apply it
public void Main(){
List<CustomClass> list = new List<CustomClass>();
CustomClass cc = new CustomClass();
cc.d1 = "v1";
cc.d2 = 0; //v2
list.Add(cc);
//to access it
foreach(CustomClass tmpClass in list)
{
string d1Value = tmpClass.d1;
int d2Value = tmpClass.d2;
}
}
I have a simple object like such:
public class Foo {
public int One { get; set; }
public int Two { get; set; }
....
public int Eleven { get; set; }
}
Given an IEnumerable, what I want is a LINQ method to transform as such:
myFooEnumerable.Select(n => transformMagicGoesHere);
Where my return object looks like this:
public class Bar {
public string DurationDescription {get;set;} //Value would be "One" or "Two" or ...
public int Value {get;set;} //Holds value in the property One or Two or ...
}
So for every item N in myFooEnumerable in the example above I'd get 11(N) items in my resultant select statement.
This should do it:
var bars = myFooEnumerable.SelectMany(
x => x.GetType().GetProperties().Select(p => new Bar {
DurationDescription = p.Name,
Value = (int)p.GetValue(x)
}));
Not a great thing to be doing in the first place, IMO, but it will at least work.