Convert an object into list or array MVC - model-view-controller

I have an MVC grid control that is passing back the collection to my controller as an object.
The object is an ArrayList of the rows from the grid and each row is a List that represents an instance of a class called lineitem that is part of a parent record class.
I am trying to load the object into the class by looping through the object array, create an instance of my lineitem class, and then add it to my parent record class parent.AddItem(lineitem). I originally had it created as follows
var items = requestData.ExtraRequestData["Items"];
However As I determined, it was an ArrayList I changed that to
List<string> mylist;
ArrayList items = (ArrayList)requestData.ExtraRequestData["Items"];
mylist = items.Cast<string>().ToList();
I am trying to convert the ArrayList into a List so I can loop through it and load it into my class.
Model.Parent parent = new Model.Parent();
Model.LineItem lineitem = new Model.LineItem();
for (int I = 0; I < mylist.length; I++){
lineitem.a = item.a,
lineitem.b = item.b
parent.AddItem(lineitem)
}
I am getting the following error on the line where I set mylist = items:
{"Unable to cast object of type 'System.Collections.Hashtable' to type 'System.String'."}
What am I missing to convert this so I can load it into my class?

Since each item in the ArrayList was a hashtable, I ended up looping through it as follows:
ParentClass ReconHdr = new ParentClass();
foreach (System.Collections.Hashtable o in items)
{
//lineItem.LineSeq = Convert.ToInt32(item.Value);
ArrayList list = new ArrayList(o.Values);
ChildClass lineItem = new ChildClass();
lineItem.StmntDocNum = list[0].ToString();
lineItem.PrtlAmt = Convert.ToDouble(list[1]);
lineItem.GLAcctCode = list[2].ToString();
lineItem.LineSeq = Convert.ToInt32(list[3]);
lineItem.ClinicID = Convert.ToInt32(list[4]);
lineItem.PrtlTranDate = Convert.ToDateTime(list[5]);
lineItem.StmntTranDate = Convert.ToDateTime(list[6]);
lineItem.PrtlFinRptDate = Convert.ToDateTime(list[7]);
lineItem.StmntEndDate = Convert.ToDateTime(list[8]);
lineItem.StmntAmt = Convert.ToDouble(list[9]);
lineItem.PrtlDocNum = list[10].ToString();
lineItem.PrtlUniqueID = list[11].ToString();
ReconHdr.AddItem(lineItem);
}

Related

Name of all the children objects in a three.js object

i want to get the var name of all the children objects in a three.js object and then store them in an array.
object has a property .children but this gets an array of all the data not just an array of the objects variable names
this is what i have tried but comes up empty
var arrayHead = headGroup.children;
var testarray =[];
for (var i = 0; i < testarray.length; i++) {
testarray.push(arrayHead[i].name);
}
if arrayHead is of type Object3D()
var testarray = [];
arrayHead.traverse ( function (child) {
testarray.push( child.name );
} );

Null pointer exception while trying to use DriverPropertyInfo[]

Code:
DriverPropertyInfo[] Information = new DriverPropertyInfo[1];
String[] Names_Arr = new String[n.size()];
Names_Arr = n.toArray(Names_Arr);
Information[0].choices = Names_Arr;
I get a "Null pointer exception" in the last line. Why?
Note: n is an ArrayList of strings.
It's not really clear what you're actually trying to accomplish, but here is a very simple example of how to use the getPropertyInfo method of the JDBC Driver object:
String connectionUrl = "jdbc:hsqldb:mem:memdb";
Properties p = new Properties();
Driver d = DriverManager.getDriver(connectionUrl);
DriverPropertyInfo[] dpi = d.getPropertyInfo(connectionUrl, p);
for (int i = 0; i < dpi.length; i++) {
System.out.println(dpi[i].name);
}
The console output displays the name of each property in the array of DriverPropertyInfo objects that getPropertyInfo returned:
user
password
get_column_name
ifexists
default_schema
shutdown
More information on the attributes returned can be found at
Class DriverPropertyInfo

Dynamically choose which properties to get using Linq

I have an MVC application with a dynamic table on one of the pages, which the users defines how many columns the table has, the columns order and where to get the data from for each field.
I have written some very bad code in order to keep it dynamic and now I would like it to be more efficient.
My problem is that I don't know how to define the columns I should get back into my IEnumerable on runtime. My main issue is that I don't know how many columns I might have.
I have a reference to a class which gets the field's text. I also have a dictionary of each field's order with the exact property It should get the data from.
My code should look something like that:
var docsRes3 = from d in docs
select new[]
{
for (int i=0; i<numOfCols; i++)
{
gen.getFieldText(d, res.FieldSourceDic[i]);
}
};
where:
docs = List from which I would like to get only specific fields
res.FieldSourceDic = Dictionary in which the key is the order of the column and the value is the property
gen.getFieldText = The function which gets the entity and the property and returns the value
Obviously, it doesn't work.
I also tried
StringBuilder fieldsSB = new StringBuilder();
for (int i = 0; i < numOfCols; i++)
{
string field = "d." + res.FieldSourceDic[i] + ".ToString()";
if (!string.IsNullOrEmpty(fieldsSB.ToString()))
{
fieldsSB.Append(",");
}
fieldsSB.Append(field);
}
var docsRes2 = from d in docs
select new[] { fieldsSB.ToString() };
It also didn't work.
The only thing that worked for me so far was:
List<string[]> docsRes = new List<string[]>();
foreach (NewOriginDocumentManagment d in docs)
{
string[] row = new string[numOfCols];
for (int i = 0; i < numOfCols; i++)
{
row[i] = gen.getFieldText(d, res.FieldSourceDic[i]);
}
docsRes.Add(row);
}
Any idea how can I pass the linq the list of fields and it'll cut the needed data out of it efficiently?
Thanks, Hoe I was clear about what I need....
Try following:
var docsRes3 = from d in docs
select (
from k in res.FieldSourceDic.Keys.Take(numOfCols)
select gen.getFieldText(d, res.FieldSourceDic[k]));
I got my answer with some help from the following link:
http://www.codeproject.com/Questions/141367/Dynamic-Columns-from-List-using-LINQ
First I created a string array of all properties:
//Creats a string of all properties as defined in the XML
//Columns order must be started at 0. No skips are allowed
StringBuilder fieldsSB = new StringBuilder();
for (int i = 0; i < numOfCols; i++)
{
string field = res.FieldSourceDic[i];
if (!string.IsNullOrEmpty(fieldsSB.ToString()))
{
fieldsSB.Append(",");
}
fieldsSB.Append(field);
}
var cols = fieldsSB.ToString().Split(',');
//Gets the data for each row dynamically
var docsRes = docs.Select(d => GetProps(d, cols));
than I created the GetProps function, which is using my own function as described in the question:
private static dynamic GetProps(object d, IEnumerable<string> props)
{
if (d == null)
{
return null;
}
DynamicGridGenerator gen = new DynamicGridGenerator();
List<string> res = new List<string>();
foreach (var p in props)
{
res.Add(gen.getFieldText(d, p));
}
return res;
}

naming objects using for loop

is there a way to name a series of objects like so:
for (i=0; i<numberOfObjectsDesired; i++;) {
Object ("thisone"+i);
thisone+i = new Object();
}
such that
thisone0, thisone1, thisone2, thisone3, ... etc are all unique instances of Objects
Sure, it's called an array:
var thisone = new object[numberOfObjectsDesired];
for (i=0; i<numberOfObjectsDesired; i++;) {
Object ("thisone"+i);
thisone[i] = new Object();
}
Just note that you have to refer to your instances as thisone[0], thisone[1], etc. instead of thisone0, thisone1, ...

Iterating tables in a context and the properties of those tables

I'm iterating the tables of a context and then the properties of those tables to eager load all columns in a context. I received some help via another question, but I don't seem to be able to figure out how to iterate the column properties of the actual table.
Final working code:
public static void DisableLazyLoading(this DataContext context)
{
DataLoadOptions options = new DataLoadOptions();
var contextTables = context.GetType().GetProperties().Where(n => n.PropertyType.Name == "Table`1");
foreach (var contextTable in contextTables)
{
var tableType = contextTable.GetValue(context, null).GetType().GetGenericArguments()[0];
var tableProperties = tableType.GetProperties().Where(n => n.PropertyType.Name != "EntitySet`1");
foreach (var tableProperty in tableProperties)
{
ParameterExpression paramExp = Expression.Parameter(tableType, "s");
Expression expr = Expression.Property(paramExp, tableProperty.Name);
options.LoadWith(Expression.Lambda(expr, paramExp));
}
}
context.LoadOptions = options;
}
You're only getting the ProperInfos. You need to get the values from the PropertyInfos:
var tablePropertInfos = context.GetType().GetProperties().Where(
n => n.PropertyType.Name == "Table`1");
foreach (var tablePropertyInfo in tablePropertInfos)
{
// Get the actual table
var table = tablePropertyInfo.GetValue(context, null);
// Do the same for the actual table properties
}
Once you have the PropertyInfo class, you need to get the value using the GetValue method.

Resources