I have a class like
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Now I have a list of this class: List<Person> persons;
var persons = new List<Person> {
new Person { Id = 1, LastName = "Reza", FirstName="Jenabi" },
new Person { Id = 1, LastName = "Amin", FirstName="Golmahalle"},
new Person { Id = 2, LastName = "Hamed", FirstName="Naeemaei"}
};
Is there a way I can group by Id and get the list of all the full Name (Combine first and last names)?
So after grouping:
var Id = results[0].Id; // Output : 1
List<string> fullNames = results[0].FullNames; // Output : "Reza Jenabi","Amin Golmahalle"
I believe this is what you need:
var results = persons.GroupBy(x => x.Id)
.Select(x => new { Id = x.Key, FullNames = x.Select(p => $"{p.FirstName} {p.LastName}").ToList() })
.ToList();
I think bellow code can help you:
var fff = from p in persons
group $"{p.FirstName} {p.LastName}" by p.Id into g
select new { PersonId = g.Key, FullNames = g.ToList() };
yeah, you can use GroupBy and Join those items:
var grouped = persons.GroupBy(p => p.Id)
.Select(s => string.Join(", ", s.Select(a=> $"{a.FirstName} {a.LastName}")));
Related
I have a model (sample below), and sample data with desired output..
I have also given two ways to populate model which will give desired output but for somereason,
looks like I am missing something....
Not able to figure out what is the issue here...
need inputs in correcting approach 1 or approach 2 or you can also suggest any other approach which will help populate model in below response..
models
class Emp
{
public int id {get;set;}
public int Name {get;set;}
public List<cardType> cardTypes {get;set;}
}
class cardType
{
public int name {get;set;}
public DateTime Expiry {get;set;}
}
sample data (Data is returned in 1 table only)
id Name cardTypeName exp
1 a Amex 1010
1 a City 2010
desired output
<Emp>
<ID>1</id>
<name>1</name>
<cardTypes>
<cardType>
<Name> Amex </Name>
<exp> 1010 </exp>
</cardType>
<cardType>
<Name> City </Name>
<exp> 2010 </exp>
</cardType>
<cardTypes>
</Emp>
approach 1
(dataTable.AsEnumerable()
.GroupBy(r => r.ItemArray[1])
.Select(grp => new Emp()
{
id = r.itemarray[1],
name = r.itemarray[1],
cardTypes = grp.Select(t => new cardType()
{
field 1,
field 2
}).ToList()
}));
approach 2
return (from DataRow dr in dataTable.Rows
select new Emp
{
id = "",
name= "",
cardTypes = dataTable.AsEnumerable()
.Select(x => new cardType
{
name = ""
exp = ""
}).ToList(),
});
try this
return dataTable.AsEnumerable()
.GroupBy(x => x.Id)
.Select(x => new
{
id = x.Key,
name = x.Key,
cardTypes = x.Select(t => new
{
name = t.Name,
exp = t.Exp
})
});
This should work for you considering you will change the Expiry field in cardType to int
var results =
dt.AsEnumerable()
.GroupBy(row => row.ItemArray[0])
.Select(rows => new Emp
{
id = rows.Key,
name = rows.Key,
cardTypes = rows.Select(row => new cardType {Name = row.ItemArray[2], Expiry = row.ItemArray[3]})
});
If you desire to have DateTime, then decide how to convert. For example:
cardTypes = rows.Select(row => new cardType {Name = row.ItemArray[2], Expiry = new DateTime(row.ItemArray[3],1,1)})
If you want to serialize to xml your models, you should mark your models with [Serializable] attribute and just use XmlSerializer.
[Serializable]
class Emp
{
public int id {get;set;}
public int Name {get;set;}
public List<cardType> cardTypes {get;set;}
}
[Serializable]
class cardType
{
public int name {get;set;}
public DateTime Expiry {get;set;}
}
After that you can use following code:
static Dictionary<int, Emp> ConvertToDict(DataTable dt)
{
Dictionary<int, Emp> emps = new Dictionary<int, Emp>();
foreach (var dr in dt.AsEnumerable())
{
var id = (int)dr["ID"];
var name = (string)dr["Name"];
var cardTypeName = (string)dr["CardTypeName"];
var exp = (int)dr["Exp"];
Emp emp;
var cardType = new CardType { Name = cardTypeName, Exp = exp };
if (emps.TryGetValue(id, out emp))
{
emp.CardTypes.Add(cardType);
}
else
{
emps.Add(id, new Emp { ID = id, Name = name, CardTypes = new List<CardType> { cardType } });
}
}
return emps;
}
static List<string> Serialize<T>(IEnumerable<T> entities) where T:new()
{
var ser = new XmlSerializer(typeof(T));
var serializedEntities = entities.Select(entity =>
{
using (var sw = new StringWriter())
{
ser.Serialize(sw, entity);
return sw.ToString();
}
}).ToList();
return serializedEntities;
}
I'm having a List<Department>, in that it has Department Name, List of Employees Name and OrderBy Dicrection for List. Now I need to construct a Dictionary<string, ObservableCollection<Employee>>, the KEY is a Department Name and the Value is an ObservableCollection by using List<Department>.
Expectation: I need to Sort the EmpName within the ObservableCollection<Employee> which is present in Dictionary<string, ObservableCollection<Employee>> EmpList based on sortEmpName Property which is in List<Department> DepList using
LINQ
I written the LINQ for this in the below Main() Function.
void Main()
{
List<Department> DepList = new List<Department>()
{
new Department() {
DepName = "HR",
sortEmpName = FilterListSortDirection.SortDirection.Ascending,
EmpName = new List<string>() {"Raj", "Baba"}
},
new Department() {
DepName = "iLab",
sortEmpName = FilterListSortDirection.SortDirection.Descending,
EmpName = new List<string>() {"Emma", "Kaliya"}
},
new Department() {
DepName = "Testing",
sortEmpName = FilterListSortDirection.SortDirection.None,
EmpName = new List<string>() {"Supriya", "Billa"}
}
};
Dictionary<string, ObservableCollection<Employee>> EmpList = DepList.Select(m =>
new {
Dep = m.DepName,
EmpCollection = new ObservableCollection<Employee>(
m.EmpName.Select(k =>
new Employee() { EmpName = k, IsChecked = true }).ToList())
}
).ToDictionary(x => x.Dep, x => x.EmpCollection);
}
The Model Classes are
public class Employee
{
public string EmpName { get; set; }
public bool IsChecked { get; set; }
}
public class Department
{
public string DepName { get; set; }
public FilterListSortDirection.SortDirection sortEmpName { get; set; }
public List<string> EmpName { get; set; }
}
public class FilterListSortDirection
{
public enum SortDirection
{
None,
Ascending,
Descending
}
}
The Output Screen Shot of List<Department> DepList
The Output Screen Shot of Dictionary<string, ObservableCollection<Employee>> EmpList
Expectation: I need to Sort the EmpName within the ObservableCollection<Employee> which is present in Dictionary<string, ObservableCollection<Employee>> EmpList based on sortEmpName Property which is in List<Department> DepList using
LINQ
This is a small part of a complex LINQ query in my project, so, I need to achieve this in LINQ. Kindly assist me.
HR => List of Employee Names should be in Ascending
iLab => List of Employee Names should be in Descending
Testing => List of Employee Names should be in original order - None (i.e., No Change - Don't Sort)
I guess you need something like this:
var EmpList = DepList.ToDictionary(p => p.DepName, p =>
{
var empList = p.EmpName.Select(k => new Employee() { EmpName = k, IsChecked = true });
if (p.sortEmpName == FilterListSortDirection.SortDirection.Ascending)
{
empList = empList.OrderBy(q => q.EmpName);
}
else if (p.sortEmpName == FilterListSortDirection.SortDirection.Descending)
{
empList = empList.OrderByDescending(q => q.EmpName);
}
return new ObservableCollection<Employee>(empList.ToList());
});
use OrderBy
Dictionary<string, ObservableCollection<Employee>> EmpList = DepList.Select(m => new
{
Dep = m.DepName,
EmpCollection =
m.sortEmpName == SortDirection.Ascending ?
new ObservableCollection<Employee>(m.EmpName.Select(k => new Employee {EmpName = k, IsChecked = true}).ToList().OrderBy(emp => emp.EmpName)) :
m.sortEmpName == SortDirection.Descending ?
new ObservableCollection<Employee>(m.EmpName.Select(k => new Employee { EmpName = k, IsChecked = true }).ToList().OrderByDescending(emp => emp.EmpName)) :
new ObservableCollection<Employee>(m.EmpName.Select(k => new Employee { EmpName = k, IsChecked = true }).ToList())
}).ToDictionary(x => x.Dep, x => x.EmpCollection);
The code below has a list with two rows in it, I want to be able to amalgamate the two lines and sum the income field based on the Country.
public class City
{
public string Name {get;set;}
public string Country {get;set;}
public double income {get;set;}
}
public class myClass
{
void Main()
{
var c = GetCities();
var d = c.GroupBy(s => new {s.Country, s.Flag, s.Name}).Select(s => new City { Country = s.Key.Country, Flag = s.Key.Flag, Name = s.Key.Name});
d.Dump(); //LINQPAD output to screen
}
public List<City> GetCities()
{
List<City> cities = new List<City>();
cities.Add(new City() { Name = "Istanbul", income= 22.00, Country = "Turkey" });
cities.Add(new City() { Name = "", income= 44.88, Country = "Turkey" });
return cities;
}
}
in my real application the list is being generated in two places, but the data needs to show on one single line.
found my answer
var result = c.GroupBy(x => x.Country)
.Select(g => {
var item = g.First();
return new{
Country = item.Country,
Name = item.Name,
income = g.Sum(x => x.income) };
}).ToList();
I know this is really basic but how do I implement a linq query that returns the most occuring field?
This is what I got so far:
var commonAge = from c in customers.GroupBy(s=>s.age)
.OrderByDescending(sg=>sg.Count())
.Take(1)
select s.Key;
Based on your comment, you are looking for the most common Age on a customer data type which has said property.
// Start with the customers collection
var mostCommonAge = customers
// then group by their age,
.GroupBy(c => c.Age,
// and project into a new anonymous type
(key, g) => new {Age = key, Count = g.Count()})
// order by count of each age
.OrderByDescending(g => g.Count)
// and take the first
.First();
Here's a complete working example. With a data model class Customer:
class Customer {
public string Name { get; set; }
public int Age { get;set; }
}
Then you can output the most common age by
static void Main(string[] args) {
var customers = new List<Customer> {
new Customer { Age = 23 },
new Customer { Age = 23 },
new Customer { Age = 23 },
new Customer { Age = 24 },
new Customer { Age = 25 }
};
var mostCommonAge = customers
.GroupBy(c => c.Age,
(key, g) => new {Age = key, Count = g.Count()})
.OrderByDescending(g => g.Count)
.First();
Console.WriteLine(mostCommonAge);
}
I am trying to load the data from datatable to objects using Linq. Below is my scenario. I have below table structure and data:
seq name id class
1 Rajesh 101 B
1 kumar 102 B
1 sandeep 104 A
2 Myur 105 B
2 Bhuvan 106 C
3 Siraz 107 A
Below is my class structures
public class student
{
public string name {get;set;}
public string id { get; set; }
public string meritClass { get; set; }
}
public class stdGroup
{
public int seqId{get;set;}
public List<student> students;
}
As a final output I should get a Student constructed for each seq. stdGroup object should be created grouping by seq [three objects].
Example:
stdGroup object 1 would contain 3 student objects
stdGroup object 2 would contain 2 student objects
Can anyone please help me.
This should do what you need (assuming by DataTable you mean DataTable):
List<stdGroup> stdGroups = myDataTable
.AsEnumerable()
.GroupBy(a => a.Field<int>("Seq"), a => new student() { id = a.Field<string>("Id"), name = a.Field<string>("name"), meritClass = a.Field<string>("class") })
.Select(a => new stdGroup() { seqId = a.Key, students = a.ToList() })
.ToList();
To break it down, firstly, get the datatable rows into a state where we can use linq,
.AsEnumerable()
Now, do the groupings - selecting seq as the key for the group, and build a student object for every entry, which will get assigned to the corresponding group.
.GroupBy(a => a.Field<int>("Seq"), a => new student() { id = a.Field<string>("Id"), name = a.Field<string>("name"), meritClass = a.Field<string>("class") })
Now, for each group create the stdGroup object, and populate the seq property from our group keys, and take the content of each group, and assign that to the students property.
.Select(a => new stdGroup() { seqId = a.Key, students = a.ToList() })
Finally, and optionally, convert to a list instead of enumerable.
.ToList();
You can also check out my implementation:
public class dbStudent
{
public int seq;
public string name;
public int id;
public string meritClass;
}
public class student
{
public string name { get; set; }
public int id { get; set; }
public string meritClass { get; set; }
}
public class stdGroup
{
public int seqId { get; set; }
public List<student> students { get; set; }
}
class Program
{
static void Main(string[] args)
{
var dbStudebts = new List<dbStudent>();
dbStudebts.Add(new dbStudent { seq = 1, name = "Rajesh", id = 101, meritClass = "B" });
dbStudebts.Add(new dbStudent { seq = 1, name = "kumar", id = 102, meritClass = "B" });
dbStudebts.Add(new dbStudent { seq = 1, name = "sandeep", id = 104, meritClass = "A" });
dbStudebts.Add(new dbStudent { seq = 2, name = "Myur", id = 105, meritClass = "B" });
dbStudebts.Add(new dbStudent { seq = 2, name = "Bhuvan", id = 106, meritClass = "C" });
dbStudebts.Add(new dbStudent { seq = 3, name = "Siraz", id = 107, meritClass = "A" });
var result = (from o in dbStudebts
group o by new { o.seq } into grouped
select new stdGroup()
{
seqId = grouped.Key.seq,
students = grouped.Select(c => new student()
{
name = c.name,
id = c.id,
meritClass = c.meritClass
}).ToList()
}).ToList();
}
}