Convert Array to custom object list c# - c#

I have a array:-
private string[][] barValues = new string[][] { new string[]{ "1.9", "5.8", "4.8", "Since Inception", "24-Jan 2014 to 24 Jun 2014" },
new string[]{"1.2", "16.5","9.8", "Year to date","01-Apr 2014 to 24-Jun 2014" },
new string[]{"11.6","28.8","23.5","Last quarter","01-Jan to 24-Jun 2014"} };
I want to convert this array into my custom list :-
List<Portfolio> list = new List<Portfolio>();
I tried doing :-
List<Portfolio> list=myArray.Cast<Portfolio>().ToList();
But I get a error:-
System.InvalidCastException: Cannot cast from source type to
destination type.
How do I do this conversion?

You will need to use the Select operator and assign your array of strings to your Portfolio object. Something like this:
myArray.Select(array => new Portfolio { Field1 = array[0], Field2 = array[1] }).ToList()

There is no "magic" conversion from string[] to your class PortFolio, you have to do it manually.
This could work:
List<Portfolio> portFolioList = barValues
.Select(sArr => new Portfolio
{
Values = sArr.Take(3).Select(double.Parse).ToList(),
Name = sArr.Skip(3).First(),
TimeSpan = sArr.Last()
}).ToList();
If you have a class like this:
public class Portfolio
{
public List<double> Values { get; set; }
public string Name { get; set; }
public string TimeSpan { get; set; }
}

Convert Array to string try this way
string[] arr = ...
List<object> list= new List<object>(arr);
foreach(object obj in arr)
list.add(obj);

var converted = barValues.Select(a => new {Value1 = a[0], Value2= a[1]}).ToArray();
This gives you an array of anonymous objects. Just replace my anonymous constructor with your constructor of Portfolio. Lambda variable a is the array that contains the string values.

Create a constructor taking the items from barValues and iterator over those.
like:
class PortFolio()
{
PortFolio(string e1, string e2, string e3, string period, string date)
{
// Copy to properties.
}
...
}
Then in the code for copying:
foreach (var barValue in barValues)
{
list.Add(new PortFolio(barValue[0], barValue[1], barValue[2], barValue[3], barValue[4]); }
}

C# is a strongly typed language. There is no out of the box way to magically cast a bunch of string into an object.
A correct way to do what you want is to first add a constructor on your Portfolio class that takes an array of strings and assign its values to your instance properties:
class Portfolio
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
public string Method { get; set; }
public string Period { get; set; }
public Portfolio(string[] values)
{
if (values != null)
{
this.Value1 = values.ElementAtOrDefault(0);
this.Value2 = values.ElementAtOrDefault(1);
this.Value3 = values.ElementAtOrDefault(2);
this.Method = values.ElementAtOrDefault(3);
this.Period = values.ElementAtOrDefault(4);
}
}
}
You can then use linq to build your list :
var portfoliosList = barValues.Select(values => new Portfolio(values)).ToList();
If required, you can do additional work in the constructor, for instance converting the values to decimal, the method to an Enum and the period as a couple of DateTime.
If you cannot modify the Portfolio class, because it's a third party component or out of your scope, you can create a factory class with a method containing that logic :
static class ProtfolioFactory
{
static public Portfolio BuildPortfolio(string[] values)
{
var portfolio = new Portfolio();
if (values != null)
{
portfolio.Value1 = values.ElementAtOrDefault(0);
portfolio.Value2 = values.ElementAtOrDefault(1);
portfolio.Value3 = values.ElementAtOrDefault(2);
portfolio.Method = values.ElementAtOrDefault(3);
portfolio.Period = values.ElementAtOrDefault(4);
}
return portfolio;
}
}
The linq query then becomes :
var list = barValues.Select(values => ProtfolioFactory.BuildPortfolio(values)).ToList();

Related

How to use constructor to initialize list of objects containing the array

right now, I use this command to initialize a list of objects and it works fine.
public class RelatedBlog
{
public string trekid { get; set; }
public string imagepath { get; set; }
public RelatedBlog(string trekid, string imagepath)
{
this.trekid = trekid;
this.imagepath = imagepath;
}
}
trek.relatedblog = new List<RelatedBlog>
{
new RelatedBlog("trekid", "../Images/image.jpg"),
};
However, lately I have decided that instead of single string as a first property, I want to have an array of several strings - with the size up to 4 (but it can be also fixed and I can enter nulls during initialization). This is the code I am using, but it doesnt work, it expects some more "(" when I call the constructor.
public class RelatedBlog
{
public string[] trekid { get; set; }
public string imagepath { get; set; }
public RelatedBlog(string[] trekid, string imagepath)
{
this.trekid = trekid;
this.imagepath = imagepath;
}
}
trek.relatedblog = new List<RelatedBlog>
{
new RelatedBlog({"string1", "string2"}, "../Images/image.jpg"),
};
Can someone advise me where I make a mistake and how to initialize this list properly. Thanks a lot
Use:
trek.relatedblog = new List<RelatedBlog>
{
new RelatedBlog(new[] {"string1", "string2"}, "../Images/image.jpg")
};
You are using implicitly typed array, the compiler can detect the type inside array but you have to inform it that you are passing an array:
var arr1 = new[] { "hello", "world" };
is equal to
var arr2 = new string [] { "hello", "world" };

parsing string for dropdownlist options

I have a string (q.ADDLOption) with values like
Select,|IE,IE|Safari,Safari|Chrome,Chrome|
I want to parse it to be the options in a dropdownlist
Optionddl oddl = q.ADDLOption.Split('|').ToList<Optionddl>(); <== this is giving error
I have also a class
public class Optionddl
{
public string text { get; set; }
public string value { get; set; }
}
This might do the trick for you
List<Optionddl> oddl = q.ADDLOption.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => new Optionddl
{
text = x.Split(',')[0],
value = x.Split(',')[1]
})
.ToList<Optionddl>();
The first problem with the code is q.ADDLOption.Split.ToList is going to return a list and not an object of Optionddl. Secondly we cannot directly convert an array of string[] to List because 'string[]' does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.Enumerable.ToList(System.Collections.Generic.IEnumerable)' has some invalid arguments would be the error. Lastly it is optional to create ToList or ToList<Optionddl>.
Hope this helps
Because Optionddl is not something can be converted to a List.
Consider this:
List<Optionddl> oddl = q.ADDLOption.Split(new string[]{'|'}).ToList<Optionddl>();
Alternatively you can create some implicit/explicit operators :
public class Optionddl
{
public string text { get; set; }
public string value { get; set; }
public static implicit operator string(Optionddl option)
{
return option.text + "," + option.value;
}
public static implicit operator Optionddl(string str)
{
string[] extracted = str.Split(",");
return new Optionddl { text = extracted[0], value = extracted[1] };
}
}
This way you can make something like :
Optionddl meOption = new Optionddl { value = "IE", text = "IE" };
string meOptionString = meOption; // result of meOptionString = "IE,IE"
meOption = meOptionString; // result of meOption = { text = "IE", value = "IE" }

Updating Custom Class in List<T>

I am trying to update a List which is a List of Interfaces to concrete classes.
I add to the List each Market type i am interested in, for this Example these Markets are A and B
I loop over all the markets, (sample provided with 3 markets A B & C, we are only interested in A and B) And determine which is of interest to us.
Once found we pass this to an extraction method too do its work and create an instance of the Correct Market_ class type.
This all works fine, but when i try to update the list with the Updates it does not get reflected in the List.
Code below, any Suggestions?
Thanks
public class Test
{
public Test()
{
TheMarkets MarketsToUpdate = new TheMarkets();
List<SpecificCompanyMarket> lstMarks = new List<SpecificCompanyMarket>();
lstMarks.Add(new SpecificCompanyMarket(1234, "A", "Some HTML DATA HERE"));
lstMarks.Add(new SpecificCompanyMarket(5874, "B", "Some HTML DATA HERE"));
lstMarks.Add(new SpecificCompanyMarket(2224, "C", "Some HTML DATA HERE"));
foreach (var item in lstMarks)
{
if (MarketsToUpdate.IsMarketWeAreInterestedIn(item.MarketName))
{
ITheMarkets MarkToUpdate = ExtractMarketData(item);
var obj = MarketsToUpdate.MarketsWeAreInterestedIn.FirstOrDefault(x => x.MarketName() == "A");
if (obj != null)
{
obj = MarkToUpdate;
}
}
}
//Look At MarketsToUpdate Now and the item has not changed, still original values
//I was expecting to see the new values for the fields in A, not the default 0's
}
public ITheMarkets ExtractMarketData(SpecificCompanyMarket item)
{
ITheMarkets market = null;
if (item.MarketName.ToUpper() == "A")
{
Market_A marketType = new Market_A();
marketType.SomeValue1 = 123;
marketType.SomeValue2 = 158253;
market = marketType;
}
//Other Market extractions here
return market;
}
}
public class SpecificCompanyMarket
{
public int MarketId { get; set; }
public string MarketName { get; set; }
public string MarketDataHTML { get; set; }
public SpecificCompanyMarket(int MID, string MName, string MData)
{
MarketId = MID;
MarketName = MName;
MarketDataHTML = MData;
}
}
public class TheMarkets
{
public List<ITheMarkets> MarketsWeAreInterestedIn = new List<ITheMarkets>();
public TheMarkets()
{
Market_A A = new Market_A();
Market_B B = new Market_B();
MarketsWeAreInterestedIn.Add(A);
MarketsWeAreInterestedIn.Add(B);
}
public bool IsMarketWeAreInterestedIn(string strMarketName)
{
bool blnRetVal = false;
foreach (var item in MarketsWeAreInterestedIn)
{
if (item.MarketName().ToUpper().Trim().Equals(strMarketName.ToUpper().Trim()))
{
blnRetVal = true;
break;
}
}
return blnRetVal;
}
}
public interface ITheMarkets
{
string MarketName();
}
public class Market_A : ITheMarkets
{
public string LabelType { get; private set; }
public double SomeValue1 { get; set; }
public double SomeValue2 { get; set; }
public double SomeValue3 { get; set; }
public Market_A()
{
LabelType = "A";
}
public string MarketName()
{
return LabelType;
}
}
public class Market_B : ITheMarkets
{
public string LabelType { get; private set; }
public List<string> SomeList { get; set; }
public double SomeValue { get; set; }
public Market_B()
{
LabelType = "B";
}
public string MarketName()
{
return LabelType;
}
}
This is a short example to get you going. Loop through your list, find the object you want to update, create a new object of that type and then find the original objects index in the list and overwrite it in place. You are essentially just replacing the object in the list with a new one not mutating the existing one.
foreach (var item in lstMarks)
{
//your code to get an object with data to update
var yourObjectToUpdate = item.GetTheOneYouWant();
//make updates
yourObjectToUpdate.SomeProperty = "New Value";
int index = lstMarks.IndexOf(item);
lstMarks[index] = yourObjectToUpdate;
}
You are extracting an obj from marketWeAreInterestedIn list using LINQ's firstOrDefault extension. This is a new object and not a reference to the obj in that list. Therefore, no updates will be reflected in the object inside that list. Try using 'indexof'
You are not storing "list of interfaces" in your list. List<T> stores an array of pointers to objects that support T interface. Once you enumerate (with Linq in your case) your list, you copy a pointer from list, which is not associated with list itself in any way. It is just a pointer to your instance.
To do what you want, you will have to build new list while enumerating the original one, adding objects to it, according to your needs, so the second list will be based on the first one but with changes applied that you need.
You can also replace specific instance at specific index instead of building new list in your code, but to do this you will need to enumerate your list with for loop and know an index for each item:
list[index] = newvalue;
But there is a third solution to update list item directly by Proxying them. This is an example
class ItemProxy : T { public T Value { get; set; } }
var list = new List<ItemProxy<MyClass>>();
list.Insert(new ItemProxy { Value = new MyClass() });
list.Insert(new ItemProxy { Value = new MyClass() });
list.Insert(new ItemProxy { Value = new MyClass() });
foreach(var item in list)
if(item // ...)
item.Value = new MyClass(); // done, pointer in the list is updated.
Third is the best case for perfomance, but it will be better to use this proxying class for something more than just proxying.

Splitting a string that is a property of an object, creating new objects from the split strings. Is there an elegant way?

My scenario :
I have an object, lets call it object1 which looks like this :
object1{
string commaSeparatedListOfIds;
DateTime time;
int passes;
...
irrelvant properties
...
}
What I wish to do is split commaSeparatedListOfIds for each id value, and save each one in it's own object (object2), with the relevant other properties.
object2{
int id;
DateTime time;
int passes;
}
This duplicates information, but for the method I want to write any other solution will be horrifically messy.
What I have tried is :
List<object2> newObjects = new List<object2>(object1.commaSeparatedListOfIds.Split(',').Select(
new object2{
id int.Parse,
time = object1.time
passes = object1.passes
}).ToList<object2>());
but this will not build.
Can anyone help me do what I wish as elegantly as possible please ? I realise it would be possible with two loops and some horrible code, but I know there's a nice looking solution out there somewhere! :)
I believe you want something like:
List<object2> newObjects = object1.commaSeparatedListOfIds.Split(',')
.Select(str =>
new object2
{
id = int.Parse(str),
time = object1.time,
passes = object1.passes
})
.ToList();
In query syntax(which i prefer when it comes to SelectMany):
var newObjects = from ob1 in object1
from strId in ob1.commaSeparatedListOfIds.Split(',')
select new object2(){
id = int.Parse(strId),
time = ob1.time,
passes = ob1.passes
};
List<object2> result = newObjects.ToList();
(mistakenly assumed that object1 is an IEnumerable<object1>)
public class O1
{
public string Ids { get; set; }
public DateTime Time { get; set; }
public int Passes { get; set; }
}
public class O2
{
public int Id { get; set; }
public DateTime Time { get; set; }
public int Passes { get; set; }
}
static void Main(string[] args)
{
var o1 = new O1();
o1.Ids = "1,2,3,4,5";
o1.Time = DateTime.Now;
o1.Passes = 42;
var results = o1.Ids.Split(',').Select(r => new O2 { Id = int.Parse(r), Time = o1.Time, Passes = o1.Passes });
foreach (var item in results)
{
Console.WriteLine("{0}: {1} {2}", item.Id, item.Time, item.Passes);
}
}
Something like that. Beware of just doing int.Parse(...), though, as you might have invalid data in your Id string.

C# How to create a list of classes

I am new to c#, but not to programming. First of, thank you so much for your help!
I want a class or struct whichever is appropriate which has 3 variables. A string, and two datetimes.
I want to create a loop which stores new classes in a list.
Something like:
for each item in dataViewer
create new class
assign variables
store class in list
next
Thank you so much for your help
You can do this easily with LINQ:
var list = dataViewer
.Select(item => new YourClass
{
StringProperty = ...,
DateTimeProperty1 = ...,
DateTimeProperty2 = ...
})
.ToList();
It lets you state your intentions (create a list of YourClass objects from each item in dataViewer) without emphasizing the mechanics behind it (loops, etc.)
Edit: If you don't require a list, just a sequence, this also looks nice using the query syntax (same meaning):
var yourClasses =
from item in dataViewer
select new YourClass
{
StringProperty = ...,
DateTimeProperty1 = ...,
DateTimeProperty2 = ...
};
Maybe something like this
var list = new List<YourClass>();
foreach(var item in dataViewer) {
var cls = new YourClass();
// Assign variables here
// cls.Test = item.Test;
list.Add(cls);
}
Try this:
public class YourClass
{
public string YourString {get; set;}
public DateTime YourDate1 {get; set;}
public DateTime YourDate2 {get; set;}
public YourClass(string s, DateTime d1, DateTime d2)
{
YourString = s;
YourDate1 = d1;
YourDate2 = d2;
}
}
public List<YourClass> Read()
{
List<YourClass> list = new List<YourClass>();
foreach(var item in dataViewer)
list.Add(new YourClass(s,d1,d2)); // Read variables from item...
return list;
}
public class Appropriate
{
public string Value { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
IList<Appropriate> list = new List<Appropriate>();
foreach(var item in dataViewer) {
list.Add(new Appropriate() {
Value = item["value"],
Start = item["start"],
End = item["end"]
});
}
IList<Appropriate> list = new List<Appropriate>();
dataViewer.ToList().ForEach(i => list.Add(new Appropriate() {
Value = item["value"],
Start = item["start"],
End = item["end"]
});
public class Foo
{
public Foo(string name, DateTime dt1, DateTime dt2)
{
Name = name;
DT1 = dt1;
DT2 = dt2;
}
public string Name { get; set; }
public DateTime DT1 { get; set; }
public DateTime DT2 { get; set; }
}
public class Example
{
public List<Foo> example(DataView dataViewer)
{
var foos = new List<Foo>();
foreach(var data in dataViewer)
{
foos.Add(new Foo(data.Name, data.DT1, data.DT2);
}
return foos;
}
}

Categories

Resources