I am trying to Concat the two array IEnumerable lists and in result view it says The underlying array is null.
IEnumerable<ObjectToConcat> result = new ArraySegment<ObjectToConcat>();
var listA = new List<ObjectToConcat>();
var a = new ObjectToConcat
{OfficialId = Guid.NewGuid(), FirstName = "A Object"};
listA.Add(a);
var b = new ObjectToConcat
{OfficialId = Guid.NewGuid(), FirstName = "B Object"};
listA.Add(b);
// Error here is result view
result = result.Concat(listA);
var c = new ObjectToConcat
{OfficialId = Guid.NewGuid(), FirstName = "C Object"};
listB.Add(c);
// Error here is result view
result = result.Concat(listB);
Can anyone please suggest me what is wrong with my code. Or Concat does not work with List?
It would appear that this code:
IEnumerable<ObjectToConcat> result = new ArraySegment<ObjectToConcat>();
is an attempt to make an empty enumerable. You can do this more effectively and clearly by writing:
IEnumerable<ObjectToConcat> result = Enumerable.Empty<ObjectToConcat>();
That said, chaining a lot of Concat calls probably isn't the most effective, performance wise, if there are a lot of sub-lists. It's probably going to perform a bit better if you create a List<IEnumerable<ObjecToConcat>> allLists, add all of the sub-lists to that list, and then at the end you can write:
result = allLists.SelectMany(x => x);
to flatten it down to just a list of items.
you can have list of ObjectToConcat like below and add items to it using addrange method
List<ObjectToConcat> result = new List<ObjectToConcat>();
result.AddRange(listA);
Try this:
IEnumerable<ObjectToConcat> result =
new ArraySegment<ObjectToConcat>(new ObjectToConcat[0]).Array;
Related
I have two hashsets that I want to except to get the distinct data:
var dataA = new HashSet<string>();
var dataB = new HashSet<string>();
var exceptA = dataA.Except(dataB);
var exceptB = dataB.Except(dataA);
var result = exceptA.Union(exceptB);
var container = new HashSet<string>(result);
dgv_B.DataSource = container.ToList();
My problem is when I insert the container into the datageid, this is my result:
And this is the raw data from breakpoints:
How can I display the text in the DGV?
The answer was given by fellow #GoodNightNerdPride:
It seems that the DGV does not work with lists of primitive types. It expects objects and analyzes their properties to generate the columns (a string has a Length property). Try something like this instead:
dgv_B.DataSource = container.Select(s => new { Value = s }).ToList();
I have the below dynamic array
dynamic[] queueList = await queueInfo.Run.GetData<dynamic[]>(Name);
I want to get the unique values present in ImportTableName field. So, after some search I found this,
var distNames = queueList.Select(o => new {o.ImportTableName }).Distinct();
which works as expected.
But I don't want to hard code the fieldname "ImportTableName". So tried doing something like this
dynamic[] queueList = await queueInfo.Run.GetData<dynamic[]>(Name);
string tableName = "ImportTableName"
var distNames = queueList.Select(o => new {o.tableName }).Distinct();
But it doesn't work, result is just NULL. Can someone help me to achieve this ?
As it is array of JObject, which has indexer, you can apply it to each element to get value of desired field:
var distNames = queueList.Select(x => x[tableName]).Distinct().ToList();
My solution is:
dynamic[] queueList = new dynamic[]{new { id = 1, ImportTableName = "Data"}};
string propName = "ImportTableName";
var distNames = queueList.Select(d => d.GetType().GetProperty(propName).GetValue(d, null)).Distinct();
It returns as expected "Data".
I have a class named DataAPIKey. I have a second class which inherits from that one.
In my code I have a List, and I would like to use that to make a list of the new class. Is there any way to do this without using a for each loop?
Using the example below I made the following code which seems to be doing what I want.
List<DataAPIKey> apiList = db.GetPendingAction("Character");
List<DataCharacter> charList = apiList.Select(k => {
DataCharacter dc = new DataCharacter(k.apiKeyId, k.keyId, k.verificationCode);
return dc;
}).ToList()
Use the LINQ Select method.
var newList = oldList.Select(oldItem => new InheritedItem(oldItem)).ToList();
In plain English this translates to "Take each item from oldList, feed each as a function parameter to a function which will take that item and perform some logic to return a different type of item, then take all the returned items and populate them into a new List."
Or if you don't have a constructor to initialize the inherited class then you can provide any code block:
var newList = oldList.Select(oldItem =>
{
var newItem = new InheritedItem();
newItem.Property = oldItem.Property;
return newItem;
}).ToList();
Or an initializer:
var newList = oldList.Select(oldItem => new InheritedItem()
{
Property = oldItem.Property,
Property2 = oldItem.Property2
}).ToList();
Example:
context means Instance of dbml.
var lst1 = context.Customers.AsQuerable();
var lst2 = context.Products.AsQuerable();
var lst3 = context.Employees.AsQuerable();
how do i return these three different result sets into single object?
i need like this type :
var lstFinalResult = lst1 + lst2 + lst3;
I'm using Linq to sql and c#.
Please any one can give the answer.
you can use anonymus types. something like this
var somethingJSON = new {
list1Data= lst1 ,
list2Data= lst2,
list3Data= lst3
};
return JSON(somethingJSON,JsonRequestBehavior.AllowGet);
Assuming sr is an IEnumerable<string>, I want to use code like this to do an inline calculation using two of the items from sr.Lines(). The problem is that the lambda is of type "lambda expression" and not a Decimal, which shares is expecting. Is there any way to do this type of inline method in an object initializer?
var trades =
from line in sr.Lines()
let items = line.Split('|')
select new Trade
{
Total = () => {
return Convert.ToDecimal(items[1]) + Convert.ToDecimal(items[2]);
},
Name = items[3]
}
You want a decimal expression, not a function:
var trades =
from line in sr.Lines()
let items = line.Split('|')
select new Trade
{
Total = Convert.ToDecimal(items[1]) + Convert.ToDecimal(items[2]),
Name = items[3]
};
Just in case someone else ends up here looking for a solution like I did.
Check How to call anonymous function in C#?.
var trades =
from line in sr.Lines()
let items = line.Split('|')
select new Trade
{
Total = ((Func<decimal>)(() =>
{
return Convert.ToDecimal(items[1]) + Convert.ToDecimal(items[2]);
}))(),
Name = items[3]
};