Replacing ArrayList with List<> in C# - c#

I am using extensively ArrayList and having difficulty to use this List<>. I am using the EntitySpace ORM for doing DAL stuff. This thing works nicely BUT the issue is that I have to defined List<> with type of object which is complaining that it can't convert that.
I am appreciate your help.
Original using ArrayList:
public ArrayList Get()
{
TndCustomerTendersCollection collection = new TndCustomerTendersCollection();
collection.Query
.Select
(
collection.Query.CustomerTenderID,
collection.Query.CustomerTenderID,
collection.Query.CustomerTenderCode,
collection.Query.CustomerTenderName,
collection.Query.StartDate,
collection.Query.DueDate,
collection.Query.CompleteDate,
collection.Query.DateCreated,
collection.Query.LastDateModified
)
.Where
(
collection.Query.IsActive.Equal(true)
);
ArrayList list = new ArrayList ();
foreach (TndCustomerTenders item in collection)
{
list.Add(item);
}
return list;
}
After replacing with List
public List<Tender> Get()
{
TndCustomerTendersCollection collection = new TndCustomerTendersCollection();
collection.Query
.Select
(
collection.Query.CustomerTenderID,
collection.Query.CustomerTenderID,
collection.Query.CustomerTenderCode,
collection.Query.CustomerTenderName,
collection.Query.StartDate,
collection.Query.DueDate,
collection.Query.CompleteDate,
collection.Query.DateCreated,
collection.Query.LastDateModified
)
.Where
(
collection.Query.IsActive.Equal(true)
);
// HOW DO CONVERT THAT TO THAT LIST
List<Tender> list = new List<Tender>();
foreach (TndCustomerTenders item in collection)
{
list.Add(item);
}
return list;
}

TndCustomerTenders and Tender are two different types.
You need to either explicitly convert from TndCustomerTenders to Tender, or you need to define an implicit conversion.
List<Tender> list = new List<Tender>();
foreach (TndCustomerTenders item in collection)
{
//assumes conversion via constructor
list.Add(new Tender(item));
}
or
List<Tender> list = new List<Tender>();
foreach (TndCustomerTenders item in collection)
{
Tender t = new Tender() { foo = item.foo, bar = item.bar };
list.Add(t);
}

return collection.ToList<Tender>();

Change
public List<Tender> Get()
to
public List<TndCustomerTenders> Get()
and change
List<Tender> list = new List<Tender>();
to
List<TndCustomerTenders> list = new List<TndCustomerTenders>();
Or if you are using the latest framework,
var list = new List<TndCustomerTenders>();

Is this what you want?
.Where(collection.Query.IsActive.Equal(true)).Cast<Tender>().ToList()

Related

Define list before without the type C#

I would like to initiate the list and depending on some value make this list with a special type which is a model that I create. I don't know if I explained it very well - my code is below
public static List<someModeltypeAorBorC> SomeMethod()
{
var list = new List<dynamic>();
if (Val == "A")
{
List<ModelA> list = new List<ModelA>();
}
else if (Val == "B")
{
List<ModelB> list = new List<ModelB>();
}
else if (Val == "C")
{
List<ModelC> list = new List<ModelC>();
}
}
I want to initialize the list outside the scope so that I can use in the next steps. Also as you can see, I have to return this List. Is it possible? I would appreciate any help!
you can try it:
static List<T> CreateList<T>(T value) {
var list = new List<T>();
list.Add(value);
return list;
}
var list = CreateList(
new{Name="Krishna", Phones = new[] {"555-555-5555", "666-666-6666"}} );
Thank You all for answer. What I finally did was creating the class Model which other classes Model A.B and C inherits from. Inside the method im adding the line
List<Model> list = new List<Model>();
I return List and when i need to reach the properties of the lower class, I am using
List<Model> obj = List<Model>;
obj = SomeMethod() //which return object of ModelA.B or C
((ModelA)obj).PropoertyOfModelA // reach the property specified model

How can I cast a list to an observablecollection in c#?

I have a List that I want to cast to an ObservableCollection, this is my code
var list = Models.Lands.FromJson(responce.Result.ToString());
this.Lands = new ObservableCollection<Land>( list );
FromJson returns me a List<Models.Land>, and this.Lands is an ObservableCollection<Models.Land>.
new ObservableCollection<Models.Land>( list ) gives me the following error:
cannot convert from System.Collections.Generic.List<> to
System.Collections.Generic.IEnumerable<>,
I thought that the constructor was overloaded for a List<> object.
Try using the nongeneric var keyword.
List<Land> list =Models.Lands.FromJson(responce.Result.ToString());
var collection = new ObservableCollection<Land>(list);
I think you can do something like below to avoid your problem but still using a single line of code:
this.Lands = new ObservableCollection<Land>( list.ToArray<Land>());
but I'm not sure if that's an efficient way to do that.
Works in my browser: https://dotnetfiddle.net/VVB8YY
public static void Main()
{
var list = new List<String>{"a", "b", "c"};
var collection = new ObservableCollection<string>(list);
foreach (var value in collection)
{
Console.WriteLine(value);
}
Console.WriteLine("Hello World");
}

Add list to another one - nested list

I would like to add some list to another list.
The result should looks like a main list contains nested list elements.
MainList:
[0]InnerList:
[0]some values
[1]some values
[1]InnerList:
[0]some values
[1]some values
I try to do this this way:
list.Add(new List<myClass>().Add(innerList));
but I've got an error message
Split it up, add does not return the list.
List<List<String>> inner = new List<List<String>>();
List<String> leaves = new List<String>();
leaves.Add( "some string" );
inner.Add( leaves );
list.Add( inner );
Try to use the list constructor instead of the Add method. Because the Add method returns void.
list.Add(new List<myClass>(innerList));
or if innerList is already of type List<myClass> then you can do:
list.Add(innerList);
List of elements of type myClass is List<myClass> so list of such lists is List<List<myClass>>. Here is a simple example how to create and populate such list of lists in one line (I used fictional type C):
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var list = new List<List<C>>{
new List<C>{
new C(1),
new C(2)
},
new List<C>{
new C(3),
new C(4)
}
};
Console.WriteLine((list[0][0]).Value);
Console.WriteLine((list[0][1]).Value);
Console.WriteLine((list[1][0]).Value);
Console.WriteLine((list[1][1]).Value);
}
}
public class C
{
public int Value;
public C(int val)
{
this.Value = val;
}
}
This will give you output:
1
2
3
4

How can I add an item to a list and return a new list

I was asked this question today:
How can I add an item to a list and return that list back?
The code for List<T>.Add(T) returns void. So you can't do something like this:
var list = new List<string>{"item1","item2"};
var newList = list.Add("item3");
This is related to using AutoMapper, although that part isn't particularly important.
One option is Linq, with Concat:
var list = new List<string>{"item1", "item2"};
var newList = list.Concat(new[] { "item3" }).ToList();
In typical Linq fashion, list stays the same, and newList contains all the items from list as well as the items in the new list, in this case just "item3".
You can skip the .ToList() to keep the IEnumerable<string> result if that fits your use case.
If you find yourself doing this often with individual items, you can use something like this extension method to pass them without the new[] { ... } syntax:
public static IEnumerable<T> ConcatItems<T>(this IEnumerable<T> source, params T[] items)
{
return source.Concat(items);
}
Because of the params array the earlier example becomes:
var list = new List<string>{"item1", "item2"};
var newList = list.ConcatItems("item3").ToList();
Make sure not to mix this up with Union, which removes duplicate items. (Searching for those duplicates is overhead that you probably don't want!)
The answer to this question was relatively simple:
var list = new List<string>(new List<string>{"item1","item2"}){"item3"};
List<T>() has a constructor that can take in IEnumerable<T> (MSDN). Additionally, you can use the object setter to put new items into the list.
So, for a more complicated example:
var originalList = new List<string>();
originalList.Add("item1");
originalList.Add("item2");
var newList = new List<string>(originalList){"item3"};
You can simply do :
List<string> list = new List<string>{"item1","item2"};
List<string> newList = null;
(newList = list.ToList()).Add("item3");
Or create your own extension method :
public static class Helper
{
public static List<T> MyAdd<T>(this List<T> collection, T item)
{
collection.Add(item);
return collection;
}
}
And use it :
List<string> list = new List<string>{"item1","item2"};
List<string> newList = list.MyAdd("item3"); // same object though
List<string> newList2 = newList.ToList().MyAdd("item4").MyAdd("item5"); // different object
One property of an ImmutableList<T> (and other similar data structures from System.Collections.Immutable) is that it doesn't mutate the original list, it returns another immutable list with the added value.
So doing this:
var originalImmutable = ImmutableList<int>.Create(1, 2);
var otherImmutable = originalImmutable.Add(3);
Will result in a shallow copied new list each time you call Add.
The most readable and maintainable solution is to copy the list and then add the item:
var list = new List<string>{"item1","item2"};
var newList = list.toList();
newList.Add("item3");
Seven years have passed since the question has been asked but Enumerable class now offers Prepend and Append methods that could be used in a straightforward fashion:
var list = new List<string>{"item1","item2"};
var newList = list.Append("item3").ToList();

convert List<List<object>> to IList<IList<object>>

I have written a method which is
public List<List<object>> Fetch(string data),
inside I create
List<List<object>> p = new List<List<object>>();
my boss now wants to return a IList<IList<object>> instead of List<List<object>> ie
public IList<IList<object>> Fetch(string data),
so when I try do
return (IList<IList<object>>) p; //throws an exception
How do I convert
List<List<object>> to IList<IList<object>> and back to List<List<object>>
You can't perform that conversion via straight casting - it wouldn't be safe. Instead, you should use:
IList<IList<object>> ret = new List<IList<object>>();
Then for each "sublist" you can use:
// Or whatever
ret.Add(new List<object>());
Finally, just return ret.
You could use LINQ to perform the conversion of your existing List<List<object>> when you return it - but it would be better to just create a more appropriate type to start with, as shown above.
To understand why some of the existing answers are wrong, suppose you could do this:
IList<IList<object>> p = new List<List<object>>();
Then this would be valid:
List<List<object>> listOfLists = new List<List<object>>();
IList<IList<object>> p = listOfLists;
p.Add(new object[]);
List<object> list = p[0];
But p[0] is a reference to an object[], not a List<object>... our supposedly type-safe code doesn't look as safe any more...
Fortunately, IList<T> is invariant to prevent exactly this problem.
You would have to declare your list as
IList<IList<object>> list = new List<IList<object>>(); // Works!
This works, because only the outer list is created in the first place. You can then insert individual items that are compatible with IList<object>:
list.Add(new List<object>());
list.Add(new object[10]);
var myOriginalList = new List<List<Object>>();
var converted = ((IList<IList<Object>>)myOriginalList.Cast<IList<Object>>().ToList()); // EDIT: Added .ToList() here
You shouldn't need to convert back- you can do just about anything on IList that you could on List.
You need to change the declaration of the result variable from List<List<object> to IList<IList<object>>
Which you can instantiate against List<IList<object>>
And each item in the result can be of type List<object>
static IList<IList<object>> Test()
{
IList<IList<object>> result = new List<IList<object>>();
var item = new List<object>() { "one", "two", "three" };
result.Add(item);
return result;
}
public IList<IList<object>> Fetch(string data)
{
IList<IList<object>> p = new List<IList<object>>();
// add your stuff here
return p;
}
List<T> and IList<T> do not support covariance. So this does not compile:
List<List<string>> list = new List<IList<string>>(); // DOES NOT COMPILE!!!!!

Categories

Resources