Storing and retrieving from lists with custom structures in - c#

I'm having some trouble storing and retrieving items into a list<> with a custom structure.
My structure looks like this:
public class list_rss_parameters
{
public string this_string { get; set; }
public string title_start { get; set; }
public string title_end { get; set; }
public string description_start { get; set; }
public string description_end { get; set; }
public string link_start { get; set; }
public string link_end { get; set; }
public string publish_date_start { get; set; }
public string publish_date_end { get; set; }
public string author_start { get; set; }
public string author_end { get; set; }
}
My stored procedure looks like this (and note that the variable names are the same as the custom Key names) Is this ok?
//this is the last part of a custom method that returns a list
List<list_rss_parameters> list_rss_items = new List<list_rss_parameters>();
list_rss_items.Add(new list_rss_parameters()
{
this_string = this_string,
title_start = title_start,
title_end = title_end,
description_start = description_start,
description_end = description_end,
link_start = link_start,
link_end = link_end,
publish_date_start = publish_date_start,
publish_date_end = publish_date_end,
author_start = author_start,
author_end = author_end
});
return list_rss_items;
If the above two setups are correct, how do I pull items out of the List once I return it?
List<list_rss_parameters> list_rss_parameters = new List<list_rss_parameters>();
list_rss_parameters = f_discover_rss_parameters(rss);
show(list_rss_parameters.Count.ToString());
show(list_rss_parameters[0].ToString()); //does not show this_string
show(list_rss_parameters[this_string'] //does not show this_string
show(list_rss_parameters[0][this_string'];//does not show this_string
What am I doing wrong?

You want the this_string property of the first item in your list it seems:
show(list_rss_parameters[0].this_string);
Or show all of them:
foreach(var item in list_rss_parameters)
{
Console.WriteLine(item.this_string);
}
As a side note your property names don't match the PascalCase naming convention for properties in .NET - so this_string really should be ThisString.

Related

dynamic to specific class object conversion

how to convert dynamic variable to specific class.
dynamic variable has the same properties as my specific class.
public class PracovnikHmotZodpovednostDropDownListItem
{
[Column("ZAZNAM_ID")]
public int? ZaznamId { get; set; }
[Column("TEXT")]
public string Text { get; set; }
[Column("VALL")]
public int Value { get; set; }
public bool Disabled { get; set; } = false;
public UpdateStatusEnum UpdateStatus { get; set; }
}
void someMethod(dynamic dtos){
List<PracovnikHmotZodpovednostDropDownListItem> dto =
(List<PracovnikHmotZodpovednostDropDownListItem>)dtos;
}
If all you know is that the properties have the same names, you're in duck typing territory, casting won't help you.
Good news is, it's trivial to do, just tedious:
var dtoList = new List<PracovnikHmotZodpovednostDropDownListItem>();
foreach(var dto in dtos)
dtoList.Add(new()
{
ZaznamId = dto.ZaznamId,
Text = dto.Text,
// etc..
});

C# Searching List of Arrays for specific value and returning related value

I hope this isn't a foolishly simple question. Im very simply trying to figure out how to manipulate a relatively simple table in SQLite through C#.
Im looking to take a parameter and search a List of Arrays for one such array where the parameter matches, and return a related variable within that same array.
For example where an array in the list might be.
Name IATA
Brisbane BNE
The sqlbind:
public static List<Airport> LoadAirports()
{
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
var output = cnn.Query<Airport>("select * from Airport", new DynamicParameters());
return output.ToList();
}
}
The Class:
class Airport
{
int Id { get; set; }
string Name { get; set; }
string LocationName { get; set; }
string IATA { get; set; }
string PortType { get; set; }
string PortOwner { get; set; }
string MotherPort { get; set; }
bool Active { get; set; }
bool IsApplyMeetAndGreet { get; set; }
decimal MeetAndGreet { get; set; }
}
The main Program:
List<Airport> Airports = new List<Airport>();
public FreightCalculator()
{
LoadAirportsList();
string OriginName = OriginInput.Value;
var OriginAirport = Airports.Where(s => s.Name == OriginName);
}
private void LoadAirportsList()
{
Airports = SqliteDataAccess.LoadAirports();
}
Ive tried various combinations of Where, Equals, For each indexing etc. Always getting an error of some kind.
The Error with the above Airports.Where is that the s.Name is inaccessible due to its protection level.
If I do:
var OriginAirport = Airports.Where(Name => Name == OriginName);
I get an error where the operand == cannot be used with Airport and String (Though Name is a string in Airport.)
Im either missing something simple or making this more complicated than it needs to be. Once I find the matching Airport, I need to return the IATA code.
Which I envisage looking like this:
var OriginIATA = OriginAirport.IATA;
Im tired and feeling dumb. Please help :(
Since you declared all members of the Airport class as properties I assume you wanted to expose them publicly.
The error you get is because they are private members and can't be accessed outside the class.
Change "Airport" class to:
class Airport
{
public int Id { get; set; }
public string Name { get; set; }
public string LocationName { get; set; }
public string IATA { get; set; }
public string PortType { get; set; }
public string PortOwner { get; set; }
public string MotherPort { get; set; }
public bool Active { get; set; }
public bool IsApplyMeetAndGreet { get; set; }
public decimal MeetAndGreet { get; set; }
}

Exclude some fields from class

I have a data class of 20 decimal fields and another of 20 string fields. I populate both classes from SQL calls then use the values to populate labels on a web page.
public class MyDecimals
{
public decimal MyDecimal1 { get; set; }
public decimal MyDecimal2 { get; set; }
public decimal MyDecimal3 { get; set; }
public decimal MyDecimal4 { get; set; }
public decimal MyDecimal5 { get; set; }
public decimal MyDecimal6 { get; set; }
.
.
public decimal MyDecimal20 { get; set; }
}
and
public class MyDecimalsNames
{
public string MyDecimal1Name { get; set; }
public string MyDecimal2Name { get; set; }
public string MyDecimal3Name { get; set; }
public string MyDecimal4Name { get; set; }
public string MyDecimal5Name { get; set; }
public string MyDecimal6Name { get; set; }
.
.
public string MyDecimal20Name { get; set; }
}
On the aspx page the labels are named like lblMyDecimal1,lblMyDecimal1Name,lblMyDecimal2, lblMyDecimal2Name.....
Rather than code the label values line by line like this ...
lblMyDecimal1.Text = myClass.MyDecimal1.ToString("#,##0");
lblMyDecimal1Name.Text = myNamesClass.MyDecimal1Name;
lblMyDecimal2.Text = myClass.MyDecimal2.ToString("#,##0");
lblMyDecimal2Name.Text = myNamesClass.MyDecimal2Name;
I am trying to loop through the fields in the class and assign the label values based on the field name:
foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(myClass))
{
((Label)FindControl("lbl" + prop.Name)).Text = prop.GetValue(myClass).ToString("#,##0");
}
foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(myNameClass))
{
((Label)FindControl("lbl" + prop.Name)).Text = prop.GetValue(myNameClass) + "";
}
This would work as I want, however at the moment I only have labels 1 - 12, 13 - 20 are not used on this particular page.
So my question is: What is the best way for me to skip the fields/labels that are not on the page?
I know I could create a list of excludes and test each value for not in list but I would like to exclude them before that as if they are not in the class in the first place if possible.
Simply check whether the label exists:
if (FindControl("lbl" + prop.Name) != null) { ... }
Or use databinding.

How to update Mongo DB by parsing a list of object

I'm using c#
one of the mongo db document is in such structure
Class QuestionData{
public Guid Id { get; set; }
public string Type { get; set; }
public List<NotesQuestion> Question { get; set; }
}
So I want to do a updating. I write the update as following:
var update = Update.Set("Question", data.Question);
The 'data' is a type of QuestionData.
But now it says "invalid argument" of data.Question.
If I change it to
var update = Update.Set("Question", data.Question.ToJson());
It has no problem. But I don't want to save it as json string.
How to resolve this issue?
With my own model classes,
internal class QuestionData
{
[BsonId] public Guid Id { get; set; }
[BsonElement("type")] public string Type { get; set; }
[BsonElement("qnotes")] public QuestionNote[] QuestionNotes { get; set; }
}
[BsonNoId] internal class QuestionNote
{
[BsonElement("q")] public string Question { get; set; }
[BsonElement("n")] public string Note { get; set; }
}
you can overwrite the whole bson array like:
// get client, database, collection ...
var col = Database.GetCollection<QuestionData>("questions");
// QuestionNote[] toModifyWith = ...
var udb = Builders<QuestionData>.Update;
var fdb = Builders<QuestionData>.Filter;
var ur = await col.UpdateManyAsync(fdb.Empty, udb.Set(x => x.QuestionNotes, toModifyWith ));

Parsing text into an object in MVC with missing data

I am getting some post data from an HTML form. The data that comes up is used to build a TextualReport object. I want to parse the various pieces of form data that come up and populate those parts of my model.
The reason i am not using a model binder is this is a fairly complex object and indices of my lists can be missing (start at 2, skip 3 etc..). I am starting with an existing object, and only replacing the properties that came up in the post.
I have the following object called TextualReport.
public class TextualReport {
public IList<Section> Sections { get; set; }
public IList<string> MiscInputs { get; set; }
public TextualReport(){
Sections = new List<Section>();
MiscInputs = new List<string>();
}
}
public class Section{
public Section(){
Id = Guid.NewGuid();
Blocks = new List<Block>();
}
public Guid Id { get; set; }
public string Heading { get; set; }
public IList<Block> Blocks { get; set; }
}
public class Block{
public Block(){
PreGraphText = new List<string>();
PreGraphInput = new List<string>();
Graphs = new List<Graph>();
PostGraphText = new List<string>();
}
public Guid Id { get; set; }
public string Heading { get; set; }
public IList<string> PreGraphText { get; set; }
public IList<Graph> Graphs { get; set; }
public IList<string> PostGraphText { get; set; }
public IList<string> PreGraphInput { get; set; }
}
public class Graph{
public string Url { get; set; }
public string Caption { get; set; }
}
From my HTML/Razor form, I get a sequence of text strings like the following:
TextualReport.Sections[1].Blocks[0].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[0].Graphs[1].Caption=
TextualReport.Sections[1].Blocks[0].Graphs[2].Caption=
TextualReport.Sections[1].Blocks[0].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[0].PreGraphInput[1]=
TextualReport.Sections[1].Blocks[1].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[1].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[1].PreGraphInput[1]=
TextualReport.Sections[1].Blocks[2].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[2].Graphs[1].Caption=
TextualReport.Sections[1].Blocks[2].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[2].PreGraphInput[1]=
TextualReport.Sections[1].Blocks[3].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[3].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[3].PreGraphInput[1]=
TextualReport.Sections[1].Blocks[4].Graphs[0].Caption=
TextualReport.Sections[1].Blocks[4].PreGraphInput[0]=
TextualReport.Sections[1].Blocks[4].PreGraphInput[1]=
What would be the best approach to populate my existing data with the various pieces that came up in the form post? I get the model from the database, then update based on the new data.

Categories