I need the possiblity to create Code in C# like this
public class SummaryA
{
public string name { get; set; }
public string surename { get: set; }
public int age { get; set;}
}
now I create an list object from the class SummaryA
List<SummaryA> list1= new List<SummaryA>();
yet I need the possibility to remove the column age from the list Summary, anyone have ideas?
I need this for some more columns, so I wish the list was dynamically or some things else.
sry for my bad english.
To completely remove the column you really need another class to store the data in, for example:
public class AgelessSummaryA
{
public string name { get; set; }
public string surename { get: set; }
}
And now you can project the first list into a new list of this class with Linq and Select:
List<AgelessSummaryA> agelessSummaries = ageSummaries
.Select(s => new AgelessSummaryA
{
name = s.name,
surename = s.surename
})
.ToList();
To use it for in a grid or for some display i guess, then you can use anonymous types where you can shape the data the way you like:
var projectedList = (from l in list1
select new
{
name = l.name,
surename = l.surename
}).ToList();
Related
I have an array of objects.
I want to copy this array of objects into another array, except one property of the first array which is an identity column. How do I do that ?
public class Lot
{
[Key]
public int LotNumber { get; set; }
public string LotName { get; set; }
}
Lot[] ExistingLots = (from l in _context.Lot.Where(c => c.NumLot == 999).Select l).ToArray();
int size = ExistingLots.Length;
Compteur[] DestinationLots = new Compteur[size];
//Copy all but the LotNumber
Array.Copy(ExistingLots , 1, DestinationLots , 1, CompteursExistants.Length-size);
Create new Lot objects using your existing values, then add these back into your DbSet:
Lot[] destinationLots = ExistingLots
.Select(el => new Lot { LotName = el.LotName })
.ToArray();
_context.Lot.Add(destinationLots);
_context.SaveChanges();
I decorate the LotNumber with Identity and it now works. I had removed it because of a data import where the column values needed to be kept.
Thanks all !
public class Lot
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int LotNumber { get; set; }
public string LotName { get; set; }
}
Every building has multiple rooms. I would like to return data from a building that includes a room number from every room owned by said building via a class buildingView, which looks like this right now (with some pseudocode):
public class buildingView
{
public int id { get; set; }
public string name { get; set; }
...
public *something* roomNumbers *something*
}
To that end, I have the following query:
buildingView building = db.buildings.Select(b => new buildingView
{
id = b.id,
name = b.name,
...
roomNumbers = *I have no idea*
})
.Where(b => b.id == id)
.SingleOrDefault();
Room numbers are all strings, if that's relevant.
My questions:
How can I get my list of room numbers?
How can I improve my question's title? I lack the vocabulary right now to do better.
Is there a another way entirely?
Thanks!
P.S. If you are going to downvote, please say why. Otherwise how will I know how to improve my questions?
Assuming you have a class like ...
class Building
{
...
public ICollection<Room> Rooms { get; set; }
}
And that BuildingView is ...
public class BuildingView
{
public int Id { get; set; }
public string Name { get; set; }
...
public IEnumerable<string> RoomNumbers { get; set; }
}
... you can get the concatenated room numbers as follows:
var buildingViews = context.Buildings
.Select(b => new BuildingView
{
Id = b.Id,
Nems = b.Name,
RoomNumbers = b.Rooms
.Select(r => r.Number)
});
Side note: it's really recommended to use C# naming conventions, like PascalCase for class and property names.
I have two lists
List<Country> list1;
List<string> list2;
public class Country
{
string Id { get; set; }
string Name { get; set; }
}
I wish to copy member "Name" to list2, how do I do that?
public class Country {
public string Id {get; set;}
public string Name {get; set;}
}
// In using method ...
{
List<Country> list1 = // assign countries
// Either
List<string> list2 = new List<string>();
list1.Select(c => c.name).ForEach(list2.Add);
// OR
var list2 = list1.Select(c => c.name).ToList();
}
Here is a tutorial about LINQ
Answer to comment about "c":
Linq makes heavy use of "Lambda-Expressions" and the "c" stands for every member of
the List (and more generically of the Enumeration). On the right side of the function operator you can have
either an Expression, an anonymous method or a delegate. The variable
name is free to choose, it could have been "country", "x" or whatever
you want. I just usually take the first letter of the class-name.
There is one more way that wasn't mentioned.
You can use the List<T> class constructor which accepts an IEnumerable<T>:
var names = new List<string>(list1.Select(x => x.Name));
Well, first of all you should change your class and make Name field as public:
public class Country
{
public string id { get; private set; }
public string name { get; private set; }
}
To create list of names you should use LINQ and select clause:
list2 = list1.Select(x => x.name).ToList();
I have a datatable with below values
id Name date
1 a 5/3/2011
1 a 6/4/2011
I want to retrieve the values with a list of associated dates for each id/name pair.
I would suggest you create a class which encapsulates all of that data, and then you can create a List<T> of the appropriate type. You'd create an instance of your new class per entry in the DataTable.
If you use a strongly-typed dataset you could use the generated DataRow type instead, if you wanted.
(It's not clear what you mean by "store in a list as single entry" - the whole table, or one entry per row?)
It's difficult to answer with out the context of the usage. Is this going to be used right a way, communicated to other parts of the system. The below assumes that it's not coomunicated to other parts of the system
var list = (from e in DataTable.Rows.AsEnumerable()
select new {
id = e["id"],
Name = e["Name"],
data = e["data"]
}).ToList()
Create a class that maps onto your table. You can use EntityFramework, LINQ to SQL, nHibernate or a custom ORM to retrieve data from the table as these objects. Select the objects and use the LINQ grouping operator to either create anonymous objects (or another class with the list of dates).
public class Foo
{
public int ID { get; set; }
public sring Name { get; set; }
public DateTime Date { get; set;
}
public class Bar
{
public int ID { get; set; }
public string Name { get; set; }
public List<DateTime> Dates { get; set; }
}
public class FooDataContext : DbContext
{
IDbSet<Foo> Foos { get; set; }
}
using (var context = new FooDataContext())
{
List<Bar> bars = context.Foos
.GroupBy( f => new { f.ID, f.Name } )
.Select( g => new Bar
{
ID = g.Key.ID,
Name = g.Key.Name,
Dates = g.Select( f => f.Date )
});
}
I am working on an application for Windows Phone platform. Run into a question.
I'd 2 different List, 1 is direct read from xml file, and the other by some calculation. And I want to merge this two lists into 1, so I can display it out.
List 1:
public class studentClass1
{
public string studentID { get; set; }
public string studentFirstName { get; set; }
public string studentLastName { get; set; }
}
List 2:
public class studentClass2
{
public string studentID { get; set; }
public string studentGradePoint { get; set; }
}
First of all, I had readout the studentClass1 via
var studentList= from query in studentIndex.Descendants("student")
select new driversClass
StudentList1 = studentList.ToList();
Secondly, I process the student Grade Point calculation on the function and output to the 2nd list :
studentClass2 SG = new studentClass2
{
studentID = thestudentID ,
studentGradePoint = thestudentGradePoint .ToString()
};
StudentList2.Add(SG);
studentListAll = StudentList1 + StudentList2
now, I want to join this two list together so that I can output to screen by calling
studentResultListBox.Itemsource = StudentListAll;
any suggestion the code how would look like?
Thanks.
Assuming you just want to combine the appropriate info from both lists (it is not totally clear from your question) - introduce a third class studentClass3 that holds all the properties you want and use a join to match instances with a matching studentID:
var studentList3 = (from s1 in studentList1
join s2 in studentList2 on s1.studentID equals s2.studentID
select new studentClass3()
{
studentFirstName = s1.studentFirstName,
studentID = s1.studentID,
studentGradePoint = s2.studentGradePoint,
studentLastName = s1.studentLastName
}).ToList();
In general this problem should be rather solved when you read in the XML than trying to combine the lists later on - having three different classes for students might be confusing. Also take a look at the recommended naming conventions, they are a little off.
Use interface, e.g.
interface IStudent
{
string studentID { get; }
// other common properties below
}
public class StudentClass1 : IStudent
{
public string studentID { get; set; }
public string studentFirstName { get; set; }
public string studentLastName { get; set; }
}
public class StudentClass2 : IStudent
{
public string studentID { get; set; }
public string studentGradePoint { get; set; }
}
and then
studentResultListBox.Itemsource = list1.Cast<IStudent>()
.Concat(list2.Cast<IStudent>())
.ToList();
Or, if the inheritance is not the case, just cast everything to System.Object (+ override object.ToString)
studentResultListBox.Itemsource = list1.Cast<object>()
.Concat(list2.Cast<object>())
.ToList();