Creating lists from another list's item in Linq [duplicate] - c#

This question already has answers here:
How to flatten nested objects with linq expression
(4 answers)
Linq nested list expression
(2 answers)
Closed 7 years ago.
I have a list of a custom car object List<Car>. The car class has two properties 'BrandName' and 'Models' as defined below.
public class Car
{
public string BrandName {get; set;}
public List<string> Models {get; set;}
}
The data for the 'List` object is populated from an API as below. It returns close to 200 rows. For illustration purposes, the object has been instantiated with two items as below. The object is returned from the API with this structure and I have no control over how the data is structured and sent.
List<Car> listCarObj = new List<Car>(){
new Car(){ BrandName = "Mercedes", Models = new List<string>(){"Class A", "Class E"}},
new Car(){ BrandName = "BMW", Models = new List<string>(){"X Series", "E Series"}}
}
How do I convert this list to an IEnumerable or another List of an anonymous type having data in the below format using Linq?
var obj = new[] {new {brand = "Mercedes", model = "Class A"},
new {brand = "Mercedes", model = "Class E"},
new {brand = "BMW", model = "X Series"},
new {brand = "BMW", model = "E Series"}};
Thanks in advance..

here is a approach with SelectMany and a nested Select
var result = listCarObj.SelectMany(x => x.Models.Select(y => new { model = y,brand = x.BrandName }));

Why don't you make a linq query, in which you select every object from the listCarObj and, for every element from the list, get all the models and return an anonymous type?
Something like:
var obj = from p in listCarObj
from q in p.Models
select new {
brand=p.BrandName,
model = q
};

You can use SelectMany to flatten your Models and then project it like this:-
var result = listCarObj.SelectMany(x => x.Models,
(carObj, model) => new {
carObj.BrandName, model
});

Related

Migrate property of objects to one list object

I have collection of objects which has property with type of collection.
How can i quick migrate objects property to the one of collection object with help Linq.
Class obj
{
list<int> aa;
List<obj> b = new List<obj>()
{
new obj()
{
aa = new List<int>(){1,2,3}
},
new obj()
{
aa = new List<int>(){4,5,6}
}
}
}
i need to migrate all properties name "aa" of objects collection to one collection.
Thanks.
Use LINQ and SelectMany:
var result = b.SelectMany(o=>o.aa).ToList();

How to properly (deep) map complex objects with C# LINQ?

I have a database with owner and vehicle tables with a one-to-many relationship. I want to get all vehicle details and map each owner to each vehicle but I must map the query to a BDO. Not sure on the LINQ syntax but I have the code below.
using (var databaseContext = new DBConnection()) {
var vehicles = (from Vehicle in databaseContext.Vehicles
select Vehicle);
return vehicles.Select(x => new VehicleBDO() {
Id = x.Id,
// ... more simple data types
Owner = new OwnerBDO(
x.Owner.Id,
x.Owner.Name)
}).ToList();
}
Creating a new ownerBDO as shown is giving me a MethodNotSupportedException with details:
Only parameterless constructors and initializers are supported in LINQ
to Entities
I'm used to Java and new to LINQ so have no idea how to do it properly, any help would be much appreciated.
It wants you to do something like this:
Owner = new OwnerBDO() { Id = x.Owner.Id, name = x.Owner.Name},
(I don't know the property names for OwnerBDO so I guessed.)

C# Linq, object definition does not contains a property

I need your help
I just wrote the following code
var anynomousObject = new { Amount = 10, weight = 20 };
List<object> ListOfAnynomous = new List<object> { anynomousObject };
var productQuery =
from prod in ListOfAnynomous
select new { prod.Amount, prod.weight }; // here it object on 'prod.Amount, prod.weight' that the object defenetion does not contains the "Amount" and "weight" properties
foreach (var v in productQuery)
{
Console.WriteLine(v.Amount, v.weight);
}
so please could you help me to solve this problem.
You need to make a class of your object definition, or using the dynamic keywork instead of boxing in object :
var anynomousObject = new { Amount = 10, weight = 20 };
List<dynamic> ListOfAnynomous = new List<dynamic> { anynomousObject };
var productQuery =
from prod in ListOfAnynomous
select new { prod.Amount, prod.weight };
foreach (var v in productQuery)
{
Console.WriteLine(v.Amount, v.weight);
}
this is because, when you box as object, the compiler doesn't know the definition of your anonymous var. Dynamic make it evaluate at runtime instead of compile-time.
The other option is to create a class or struct.
Your List<object> has a list of objects. The Linq query looks this list, and all it sees are regular objects.
Either use a class or a structure to store your objects, or use List<dynamic>

Make a List of objects using an existing list

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();

LINQ and creating NON anonymous return values

I think I understand returning records of an anonymous type from But in this I want to create NEW CatalogEntries, and set them from the values selected. (context is a Devart LinqConnect database context, which lets me grab a view).
My solution works, but it seems clumsy. I want to do this in one from statement.
var query = from it in context.Viewbostons
select it;
foreach (GPLContext.Viewboston item in query)
{
CatalogEntry card = new CatalogEntry();
card.idx = item.Idx;
card.product = item.Product;
card.size = (long)item.SizeBytes;
card.date = item.Date.ToString();
card.type = item.Type;
card.classification = item.Classification;
card.distributor = item.Distributor;
card.egplDate = item.EgplDate.ToString();
card.classificationVal = (int)item.ClassificationInt;
card.handling = item.Handling;
card.creator = item.Creator;
card.datum = item.Datum;
card.elevation = (int)item.ElevationFt;
card.description = item.Description;
card.dirLocation = item.DoLocation;
card.bbox = item.Bbox;
card.uniqID = item.UniqId;
values.Add(card);
}
CatalogResults response = new CatalogResults();
I just tried this:
var query2 = from item in context.Viewbostons
select new CatalogResults
{ item.Idx,
item.Product,
(long)item.SizeBytes,
item.Date.ToString(),
item.Type,
item.Classification,
item.Distributor,
item.EgplDate.ToString(),
(int)item.ClassificationInt,
item.Handling,
item.Creator,
item.Datum,
(int)item.ElevationFt,
item.Description,
item.DoLocation,
item.Bbox,
item.UniqId
};
But I get the following error:
Error 79 Cannot initialize type 'CatalogService.CatalogResults' with a
collection initializer because it does not implement
'System.Collections.IEnumerable' C:\Users\ysg4206\Documents\Visual
Studio
2010\Projects\CatalogService\CatalogService\CatalogService.svc.cs 91 25 CatalogService
I should tell you what the definition of the CatalogResults is that I want to return:
[DataContract]
public class CatalogResults
{
CatalogEntry[] _results;
[DataMember]
public CatalogEntry[] results
{
get { return _results; }
set { _results = value; }
}
}
My mind is dull today, apologies to all. You are being helpful. The end result is going to be serialized by WCF to a JSON structure, I need the array wrapped in a object with some information about size, etc.
Since .NET 3.0 you can use object initializer like shown below:
var catalogResults = new CatalogResults
{
results = context.Viewbostons
.Select(it => new CatalogEntry
{
idx = it.Idx,
product = it.Product,
...
})
.ToArray()
};
So if this is only one place where you are using CatalogEntry property setters - make all properties read-only so CatalogEntry will be immutable.
MSDN, Object initializer:
Object initializers let you assign values to any accessible fields or properties of an
object at creation time without having to explicitly invoke a constructor.
The trick here is to create a IQueryable, and then take the FirstOrDefault() value as your response (if you want a single response) or ToArray() (if you want an array). The error you are getting (Error 79 Cannot initialize type 'CatalogService.CatalogResults' with a collection initializer because it does not implement 'System.Collections.IEnumerable') is because you're trying to create an IEnumerable within the CatalogEntry object (by referencing the item variable).
var response = (from item in context.Viewbostons
select new CatalogEntry()
{
idx = item.Idx,
product = item.Product,
size = (long)item.SizeBytes,
...
}).ToArray();
You don't have to create anonymous types in a Linq select. You can specify your real type.
var query = context.Viewbostons.Select( it =>
new CatalogEntry
{
idx = it.idx,
... etc
});
This should work:
var query = from it in context.Viewbostons
select new CatalogEntry()
{
// ...
};

Categories