I've been searching the difference between Select and SelectMany but I haven't been able to find a suitable answer. I need to learn the difference when using LINQ To SQL but all I've found are standard array examples.
Can someone provide a LINQ To SQL example?
SelectMany flattens queries that return lists of lists. For example
public class PhoneNumber
{
public string Number { get; set; }
}
public class Person
{
public IEnumerable<PhoneNumber> PhoneNumbers { get; set; }
public string Name { get; set; }
}
IEnumerable<Person> people = new List<Person>();
// Select gets a list of lists of phone numbers
IEnumerable<IEnumerable<PhoneNumber>> phoneLists = people.Select(p => p.PhoneNumbers);
// SelectMany flattens it to just a list of phone numbers.
IEnumerable<PhoneNumber> phoneNumbers = people.SelectMany(p => p.PhoneNumbers);
// And to include data from the parent in the result:
// pass an expression to the second parameter (resultSelector) in the overload:
var directory = people
.SelectMany(p => p.PhoneNumbers,
(parent, child) => new { parent.Name, child.Number });
Live Demo on .NET Fiddle
Select many is like cross join operation in SQL where it takes the cross product.
For example if we have
Set A={a,b,c}
Set B={x,y}
Select many can be used to get the following set
{ (x,a) , (x,b) , (x,c) , (y,a) , (y,b) , (y,c) }
Note that here we take the all the possible combinations that can be made from the elements of set A and set B.
Here is a LINQ example you can try
List<string> animals = new List<string>() { "cat", "dog", "donkey" };
List<int> number = new List<int>() { 10, 20 };
var mix = number.SelectMany(num => animals, (n, a) => new { n, a });
the mix will have following elements in flat structure like
{(10,cat), (10,dog), (10,donkey), (20,cat), (20,dog), (20,donkey)}
var players = db.SoccerTeams.Where(c => c.Country == "Spain")
.SelectMany(c => c.players);
foreach(var player in players)
{
Console.WriteLine(player.LastName);
}
De Gea
Alba
Costa
Villa
Busquets
...
SelectMany() lets you collapse a multidimensional sequence in a way that would otherwise require a second Select() or loop.
More details at this blog post.
There are several overloads to SelectMany. One of them allows you to keep trace of any relationship between parent and children while traversing the hierarchy.
Example: suppose you have the following structure: League -> Teams -> Player.
You can easily return a flat collection of players. However you may lose any reference to the team the player is part of.
Fortunately there is an overload for such purpose:
var teamsAndTheirLeagues =
from helper in leagues.SelectMany
( l => l.Teams
, ( league, team ) => new { league, team } )
where helper.team.Players.Count > 2
&& helper.league.Teams.Count < 10
select new
{ LeagueID = helper.league.ID
, Team = helper.team
};
The previous example is taken from Dan's IK blog. I strongly recommend you take a look at it.
I understand SelectMany to work like a join shortcut.
So you can:
var orders = customers
.Where(c => c.CustomerName == "Acme")
.SelectMany(c => c.Orders);
The SelectMany() method is used to flatten a sequence in which each of the elements of the sequence is a separate.
I have class user same like this
class User
{
public string UserName { get; set; }
public List<string> Roles { get; set; }
}
main:
var users = new List<User>
{
new User { UserName = "Reza" , Roles = new List<string>{"Superadmin" } },
new User { UserName = "Amin" , Roles = new List<string>{"Guest","Reseption" } },
new User { UserName = "Nima" , Roles = new List<string>{"Nurse","Guest" } },
};
var query = users.SelectMany(user => user.Roles, (user, role) => new { user.UserName, role });
foreach (var obj in query)
{
Console.WriteLine(obj);
}
//output
//{ UserName = Reza, role = Superadmin }
//{ UserName = Amin, role = Guest }
//{ UserName = Amin, role = Reseption }
//{ UserName = Nima, role = Nurse }
//{ UserName = Nima, role = Guest }
You can use operations on any item of sequence
int[][] numbers = {
new[] {1, 2, 3},
new[] {4},
new[] {5, 6 , 6 , 2 , 7, 8},
new[] {12, 14}
};
IEnumerable<int> result = numbers
.SelectMany(array => array.Distinct())
.OrderBy(x => x);
//output
//{ 1, 2 , 2 , 3, 4, 5, 6, 7, 8, 12, 14 }
List<List<int>> numbers = new List<List<int>> {
new List<int> {1, 2, 3},
new List<int> {12},
new List<int> {5, 6, 5, 7},
new List<int> {10, 10, 10, 12}
};
IEnumerable<int> result = numbers
.SelectMany(list => list)
.Distinct()
.OrderBy(x=>x);
//output
// { 1, 2, 3, 5, 6, 7, 10, 12 }
Select is a simple one-to-one projection from source element to a result element. Select-
Many is used when there are multiple from clauses in a query expression: each element in the original sequence is used to generate a new sequence.
The formal description for SelectMany() is:
Projects each element of a sequence to an IEnumerable and flattens
the resulting sequences into one sequence.
SelectMany() flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein.
class PetOwner
{
public string Name { get; set; }
public List<String> Pets { get; set; }
}
public static void SelectManyEx()
{
PetOwner[] petOwners =
{ new PetOwner { Name="Higa, Sidney",
Pets = new List<string>{ "Scruffy", "Sam" } },
new PetOwner { Name="Ashkenazi, Ronen",
Pets = new List<string>{ "Walker", "Sugar" } },
new PetOwner { Name="Price, Vernette",
Pets = new List<string>{ "Scratches", "Diesel" } } };
// Query using SelectMany().
IEnumerable<string> query1 = petOwners.SelectMany(petOwner => petOwner.Pets);
Console.WriteLine("Using SelectMany():");
// Only one foreach loop is required to iterate
// through the results since it is a
// one-dimensional collection.
foreach (string pet in query1)
{
Console.WriteLine(pet);
}
// This code shows how to use Select()
// instead of SelectMany().
IEnumerable<List<String>> query2 =
petOwners.Select(petOwner => petOwner.Pets);
Console.WriteLine("\nUsing Select():");
// Notice that two foreach loops are required to
// iterate through the results
// because the query returns a collection of arrays.
foreach (List<String> petList in query2)
{
foreach (string pet in petList)
{
Console.WriteLine(pet);
}
Console.WriteLine();
}
}
/*
This code produces the following output:
Using SelectMany():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
Using Select():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel
*/
The main difference is the result of each method while SelectMany() returns a flattern results; the Select() returns a list of list instead of a flattern result set.
Therefor the result of SelectMany is a list like
{Scruffy, Sam , Walker, Sugar, Scratches , Diesel}
which you can iterate each item by just one foreach. But with the result of select you need an extra foreach loop to iterate through the results because the query returns a collection of arrays.
Some SelectMany may not be necessary. Below 2 queries give the same result.
Customers.Where(c=>c.Name=="Tom").SelectMany(c=>c.Orders)
Orders.Where(o=>o.Customer.Name=="Tom")
For 1-to-Many relationship,
if Start from "1", SelectMany is needed, it flattens the many.
if Start from "Many", SelectMany is not needed. (still be able to filter from "1", also this is simpler than below standard join query)
from o in Orders
join c in Customers on o.CustomerID equals c.ID
where c.Name == "Tom"
select o
Just for an alternate view that may help some functional programmers out there:
Select is map
SelectMany is bind (or flatMap for your Scala/Kotlin people)
Without getting too technical - database with many Organizations, each with many Users:-
var orgId = "123456789";
var userList1 = db.Organizations
.Where(a => a.OrganizationId == orgId)
.SelectMany(a => a.Users)
.ToList();
var userList2 = db.Users
.Where(a => a.OrganizationId == orgId)
.ToList();
both return the same ApplicationUser list for the selected Organization.
The first "projects" from Organization to Users, the second queries the Users table directly.
It's more clear when the query return a string (an array of char):
For example if the list 'Fruits' contains 'apple'
'Select' returns the string:
Fruits.Select(s=>s)
[0]: "apple"
'SelectMany' flattens the string:
Fruits.SelectMany(s=>s)
[0]: 97 'a'
[1]: 112 'p'
[2]: 112 'p'
[3]: 108 'l'
[4]: 101 'e'
Consider this example :
var array = new string[2]
{
"I like what I like",
"I like what you like"
};
//query1 returns two elements sth like this:
//fisrt element would be array[5] :[0] = "I" "like" "what" "I" "like"
//second element would be array[5] :[1] = "I" "like" "what" "you" "like"
IEnumerable<string[]> query1 = array.Select(s => s.Split(' ')).Distinct();
//query2 return back flat result sth like this :
// "I" "like" "what" "you"
IEnumerable<string> query2 = array.SelectMany(s => s.Split(' ')).Distinct();
So as you see duplicate values like "I" or "like" have been removed from query2 because "SelectMany" flattens and projects across multiple sequences.
But query1 returns sequence of string arrays. and since there are two different arrays in query1 (first and second element), nothing would be removed.
The SelectMany method knocks down an IEnumerable<IEnumerable<T>> into an IEnumerable<T>, like communism, every element is behaved in the same manner(a stupid guy has same rights of a genious one).
var words = new [] { "a,b,c", "d,e", "f" };
var splitAndCombine = words.SelectMany(x => x.Split(','));
// returns { "a", "b", "c", "d", "e", "f" }
One more example how SelectMany + Select can be used in order to accumulate sub array objects data.
Suppose we have users with they phones:
class Phone {
public string BasePart = "555-xxx-xxx";
}
class User {
public string Name = "Xxxxx";
public List<Phone> Phones;
}
Now we need to select all phones' BaseParts of all users:
var usersArray = new List<User>(); // array of arrays
List<string> allBaseParts = usersArray.SelectMany(ua => ua.Phones).Select(p => p.BasePart).ToList();
Suppose you have an array of countries
var countries = new[] { "France", "Italy" };
If you perform Select on countries, you will get each element of the array as IEnumerable<T>
IEnumerable<string> selectQuery = countries.Select(country => country);
In the above code, the country represents a string that refers to each country in the array. now iterate over selectQuery to get countries:
foreach(var country in selectQuery)
Console.WriteLine(country);
// output
//
// France
// Italy
If you want to print every character of countries you have to use nested foreach
foreach (var country in selectQuery)
{
foreach (var charOfCountry in country)
{
Console.Write(charOfCountry + ", ");
}
}
// output
// F, r, a, n, c, e, I, t, a, l, y,
OK. now try to perform SelectMany on countries. This time SelectMany gets each country as string (as before) and because of string type is a collection of chars, SelectMany tries to divide each country into its constituent parts (chars) and then returns a collection of chars as IEnumerable<T>
IEnumerable<char> selectManyQuery = countries.SelectMany(country => country);
In the above code, the country represents a string that refers to each country in the array as before, but the return value is the chars of each country
Actually SelectMany likes to fetch two levels inside of collections and flatten the second level as IEnumerable<T>
Now iterate over selectManyQuery to get chars of each country:
foreach(var charOfCountry in selectManyQuery)
Console.Write(charOfCountry + ", ");
// output
// F, r, a, n, c, e, I, t, a, l, y,
Here is a code example with an initialized small collection for testing:
class Program
{
static void Main(string[] args)
{
List<Order> orders = new List<Order>
{
new Order
{
OrderID = "orderID1",
OrderLines = new List<OrderLine>
{
new OrderLine
{
ProductSKU = "SKU1",
Quantity = 1
},
new OrderLine
{
ProductSKU = "SKU2",
Quantity = 2
},
new OrderLine
{
ProductSKU = "SKU3",
Quantity = 3
}
}
},
new Order
{
OrderID = "orderID2",
OrderLines = new List<OrderLine>
{
new OrderLine
{
ProductSKU = "SKU4",
Quantity = 4
},
new OrderLine
{
ProductSKU = "SKU5",
Quantity = 5
}
}
}
};
//required result is the list of all SKUs in orders
List<string> allSKUs = new List<string>();
//With Select case 2 foreach loops are required
var flattenedOrdersLinesSelectCase = orders.Select(o => o.OrderLines);
foreach (var flattenedOrderLine in flattenedOrdersLinesSelectCase)
{
foreach (OrderLine orderLine in flattenedOrderLine)
{
allSKUs.Add(orderLine.ProductSKU);
}
}
//With SelectMany case only one foreach loop is required
allSKUs = new List<string>();
var flattenedOrdersLinesSelectManyCase = orders.SelectMany(o => o.OrderLines);
foreach (var flattenedOrderLine in flattenedOrdersLinesSelectManyCase)
{
allSKUs.Add(flattenedOrderLine.ProductSKU);
}
//If the required result is flattened list which has OrderID, ProductSKU and Quantity,
//SelectMany with selector is very helpful to get the required result
//and allows avoiding own For loops what according to my experience do code faster when
// hundreds of thousands of data rows must be operated
List<OrderLineForReport> ordersLinesForReport = (List<OrderLineForReport>)orders.SelectMany(o => o.OrderLines,
(o, ol) => new OrderLineForReport
{
OrderID = o.OrderID,
ProductSKU = ol.ProductSKU,
Quantity = ol.Quantity
}).ToList();
}
}
class Order
{
public string OrderID { get; set; }
public List<OrderLine> OrderLines { get; set; }
}
class OrderLine
{
public string ProductSKU { get; set; }
public int Quantity { get; set; }
}
class OrderLineForReport
{
public string OrderID { get; set; }
public string ProductSKU { get; set; }
public int Quantity { get; set; }
}
A select operator is used to select value from a collection and SelectMany operator is used to selecting values from a collection of collection i.e. nested collection.
It is the best way to understand i think.
var query =
Enumerable
.Range(1, 10)
.SelectMany(ints => Enumerable.Range(1, 10), (a, b) => $"{a} * {b} = {a * b}")
.ToArray();
Console.WriteLine(string.Join(Environment.NewLine, query));
Console.Read();
Multiplication Table example.
Related
public enum Department{ Accounts, Technology, Architecture, MBA };
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }
public int ExamScores;
public Department dep;
}
I have a list of students. How should I display students who got top marks from each departments using LINQ.
I've tried but couldn't succeed.Please suggest.
As commented it's a good idea to always include what you have tried. Both because it shows you've made an effort and it's possible for someone to help pointing out where you went wrong, which you'll learn more from.
That being said, you can solve this problem by grouping in two steps:
Group by dep
Then by ExamScores
And lastly order the results to get those with the highest scores:
// "one"-liner:
var results = students.GroupBy(s => s.dep)
.Select(depGroup => depGroup.GroupBy(s => s.ExamScores)
.OrderByDescending(scoreGroup => scoreGroup.Key) // Key is the ExamScores
.First()); // First = Get the first group, which has the highest score
See this fiddle for an example.
// Example output:
// ID dep ExamScores
// 3 Accounts 9
// 7 Accounts 9
// 4 Technology 8
// 6 Architecture 6
You can group the students by department, then find all the students in each group who have a score that matches the highest score for that department:
var departmentStudents = students.GroupBy(s => s.dep);
foreach (var department in departmentStudents)
{
var highScore = department.Max(ds => ds.ExamScores);
var bestStudents = department
.Where(student => student.ExamScores == highScore)
.Select(student => $"{student.FirstName} {student.LastName}");
Console.WriteLine($"Sudents with best score in the {department.Key} Dept.:");
Console.WriteLine($" - {string.Join("\r\n - ", bestStudents)}");
}
Suppose you have the following list of students:
List<Student> students = new List<Student> {
new Student("x", "y", 1, 10, Department.Accounts),
new Student("p", "q", 2,20, Department.Accounts),
new Student("p2", "q2", 2, 20, Department.Accounts),
new Student("a", "b", 3, 30, Department.Technology),
new Student("m", "n", 4, 40, Department.Technology),
new Student("m2", "n2", 4, 40, Department.Technology),
};
Now you can group by departments and find the students with highest marks by following approach:
var groupsByDept = students.GroupBy(a => (a.dep.ToString()));
List<Student> studentsByHighestMarksAndDeptList = groupsByDept.SelectMany(a => a.Where(b => b.ExamScores == a.Max(c => c.ExamScores))).ToList();
This will give the following result:
I have a class defined as such:
[DataContract]
public class Response2
{
[DataMember(Name = "done")]
public bool done;
[DataMember(Name = "records")]
public List<Response3> r3entry;
}
[DataContract]
public class Response3
{
[DataMember(Name = "Id")]
public string strId { get; set; }
[DataMember(Name = "Name")]
public string strName { get; set; }
}
Now, what I'd like to happen is take the values from ANOTHER class and populate... something like this:
string propertyRequest2 = CreatePropertyRequest2();
Response2 propResponse2 = MakeRequest2(propertyRequest2, sfToken);
List<Response> listAllData = new List<Response>();
foreach (var responseEntry in propResponse2.r3entry)
{
listAllData.Add(new Response() { strId = responseEntry.strId, strName = responseEntry.strName } );
// NOTE .strId IS ALWAYS UNIQUE IN BOTH CLASSES
// - I know this is NOT the right syntax... will fix later.
Where listAllData.strId = responseEntry.strId
{
listAllData.property = propertyResponse2(.strId=responseEntry.strId).property
}
}
I'm sure (at least) the last bit of the code is painful to most people reading this, but I will fix so it's not so terrible. I just don't know if explaining it is as clear. Just in case I'm wrong, the point here is more like this:
// WE HAVE A LIST OF CLASSES WITH PROPERTIES
// ASSUME PROPERTIES ARE ID, ITEM, NAME
LIST1 = { ("1", "A", "APPLE"), ("2", "B", "BANANA"), ("3", "C", "COCONUT")}
// NOW WE HAVE ANOTHER LIST THAT HAS THE SAME ID BUT DIFF DATA
// ASSUME PROPERTIES ARE ID, COLOR
LIST2 = { ("1", "RED"), ("2", "YELLOW"), ("3", "BROWN) }
// AND THEN I WANT TO CREATE A NEW LIST WITH BOTH SETS OF DATA COMBINED
// ASSUME PROPERTIES ARE ID, ITEM, NAME, COLOR
LIST3 = { ("1", "A", "APPLE", "RED"), ("2", "B", "BANANA"), ("3", "C", "COCONUT", "BROWN") }
Any ideas on how to do this?
Inner join with projection:
var list = from l1 in LIST1
join l2 in LIST2 on l1.ID equals l2.ID
select new {
l1.ID,
l1.Item,
l1.Name,
l2.Color
}
.ToList()
Without knowing exactly what your goal is, the Zip function acts on two lists and returns the product of those two lists. You can read about it more here:
Zip on MSDN
But in effect, if you have two lists of things, lets say:
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };
// The following example concatenates corresponding elements of the
// two input sequences.
var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);
So in your case it might be something like this:
Response2[] response2 = //some list
Response3[] response3= // some other list
var response2Andresponse3 = response2.Zip(response3, (res2,res3) => //something you want to do with them
This will pair the first element of response2 with the first element of response3 and so on. We are also assuming here that they have the same length so that you don't have unpaired attributes.
You'll have to figure out a way to pipe this out to something you might find useful, but you will have a list where they correspond element by element.
How can I use LINQ or Lambda instead of nested and multiple foreach statements.
I want to use a better syntax than nested foreach statements to overwrite the initial list with items from the second list.
In the code below:
I want to overwrite initialList with those in secondList that have the same Value. (Remove Red)
Use the items in secondList where Value was the same (Yellow)
New initialList list should include (Green and Yellow)
static void Main(string[] args)
{
int useProd = 2;
int useDomain = 0;
var person1 = new Person() { prodId = 1, Value = "foo", domainId = 0, Name = "Red" };
var person2 = new Person() { prodId = 1, Value = "bar", domainId = 0, Name = "Green" };
var person3 = new Person() { prodId = 1, Value = "foo", domainId = 1, Name = "Yellow" };
var initialList = new List<Person>();
initialList.Add(person1);
initialList.Add(person2);
var secondList = new List<Person>();
secondList.Add(person3);
List<Person> personsToRemove = new List<Person>();
List<Person> personsToUpdate = new List<Person>();
foreach (var pers1 in initialList)
{
foreach (var pers2 in secondList)
{
if (pers1.Value == pers2.Value)
{
personsToRemove.Add(pers1);
personsToUpdate.Add(pers2);
}
}
}
foreach (var remPers in personsToRemove)
{
initialList.Remove(remPers);
}
foreach (var updPers in personsToUpdate)
{
initialList.Add(updPers);
}
foreach (var item in initialList)
{
Console.WriteLine(String.Format("Value: {0}, prodId: {1}, domainId: {2}, Name: {3}", item.Value, item.prodId, item.domainId, item.Name));
}
Console.ReadKey();
}
public class Person
{
public int prodId { get; set; }
public string Value { get; set; }
public int domainId { get; set; }
public string Name { get; set; }
}
Your nested loops are most efficiently represented with a join. In addition, it would be helpful efficiency-wise to not have to do a linear-search on the entire list just to remove an element and then to add a new one in. There is an overload of Enumerable.Select() we can use to embed the item index in the result, so that the element can simply be replaced directly.
Putting it all together, it looks like this:
var join = from p1 in initialList.Select((p, i) => new { Person = p, Index = i })
join p2 in secondList on p1.Person.Value equals p2.Value
select new { Index = p1.Index, Replacement = p2 };
foreach (var item in join.ToList())
{
initialList[item.Index] = item.Replacement;
}
The above code replaces the original code starting with the declarations of the personsToRemove and personsToUpdate lists, and the first three foreach loops (i.e. all but the one that displays the final result).
Notes:
From the initialList, the code synthesizes an anonymous type containing the Person instance and the index of that instance in the list.
The join clause pairs up all of the items from each list where the Value properties are equal.
Important: if there are multiple elements in either list with the same Value property, they are each paired with every other element in the other list having that same Value. I.e. if initialList has two elements having the Value of "foo" and secondList has three such elements, you will wind up with six elements in the resulting join. Your question does not define whether this is possible, nor what you would want to happen if it were, so I've just ignored that possibility here. :)
The join result is projected to a new anonymous type containing the index of the element to be replaced, and the new value.
The query result is materialized by calling ToList(). This is necessary because the join is otherwise deferred and modifying the initialList would invalidate the query.
Of course, in the remaining foreach all that the code then needs to do is assign to the appropriate index position in the list the replacement value determined by the query.
You can use Generics as well. Below is the short code will work for you:
initialList.ForEach(p =>
{
if (secondList.Any(sp => sp.Value == p.Value))
{
initialList.Remove(p);
initialList.Add(secondList.Single(spu => spu.Value == p.Value));
};
});
In this example class IcdPatient represents a many-to-many relationship between a Patient table (not shown in this example) and a lookup table Icd.
public class IcdPatient
{
public int PatientId { get; set; }
public int ConditionCode { get; set; }
public static List<IcdPatient> GetIcdPatientList()
{
return new List<IcdPatient>()
{
new IcdPatient { PatientId = 100, ConditionCode = 111 },
new IcdPatient { PatientId = 100, ConditionCode = 222 },
new IcdPatient { PatientId = 200, ConditionCode = 111 },
new IcdPatient { PatientId = 200, ConditionCode = 222 },
new IcdPatient { PatientId = 3, ConditionCode = 222 },
};
}
}
public class Icd
{
public int ConditionCode { get; set; }
public string ConditionName { get; set; }
public static List<Icd> GetIcdList()
{
return new List<Icd>()
{
new Icd() { ConditionCode =111, ConditionName ="Condition 1"},
new Icd() { ConditionCode =222, ConditionName ="Condition 2"},
};
}
}
I would like for the user to be able to enter as many conditions as they want, and get a LINQ object back that tells them how many PatientIds satisfy that query. I've come up with:
List<string> stringFilteredList = new List<string> { "Condition 1", "Condition 2" };
List<int> filteringList = new List<int> { 111,222 };
var manyToMany = IcdPatient.GetIcdPatientList();
var icdList = Icd.GetIcdList();
/*Working method without joining on the lookup table*/
var grouped = from m in manyToMany
group m by m.PatientId into g
where g.Count() == filteringList.Distinct().Count()
select new
{
PatientId = g.Key,
Count = g.Count()
};
/*End*/
foreach (var item in grouped)
{
Console.WriteLine(item.PatientId);
}
Let's say that IcdPatient has a composite primary key on both fields, so we know that each row is unique. If we find the distinct number of entries in filteringList and do a count on the number of times a PatientId shows up, that means we've found all the people who have all conditions. Because the codes can be esoteric, I would like to do something like
let the user table in the ConditionName in type Icd and perform the same operation. I've not used LINQ this way a lot and I've gathered:
List<int> filteringList = new List<int> { 111,222 };
List<string> stringFilteredList= new List<string>{"Condition 1","Condition 2" };
filteringList.Distinct();
var manyToMany = IcdPatient.GetIcdPatientList();
var icdList = Icd.GetIcdList();
/*Working method without joining on the lookup table*/
var grouped = from m in manyToMany
join i in icdList on
m.ConditionCode equals i.ConditionCode
//group m by m.PatientId into g
group new {m,i} by new { m.ConditionCode }into g
where g.Count() == filteringList.Distinct().Count()
select new
{
Condition = g.Key.ConditionCode
};
/*End*/
but can't get anything to work. This is essentially a join on top of my first query, but I'm not getting what I need to group on.
You don't need to group anything in this case, just use a join and a contains:
List<string> stringFilteredList= new List<string>{"Condition 1","Condition 2" };
var patients =
from icd in Icd.GetIcdList()
join patient in IcdPatient.GetIcdPatientList() on icd.ConditionCode equals patient.ConditionCode
where stringFilteredList.Contains(icd.ConditionName)
select patient.PatientId;
Let's say that IcdPatient has a composite primary key on both fields, so we know that each row is unique. If we find the distinct number of entries in filteringList and do a count on the number of times a PatientId shows up, that means we've found all the people who have all conditions. Because the codes can be esoteric, I would like to do something like let the user table in the ConditionName in type Icd and perform the same operation.
I believe you're asking:
Given a list of ConditionCodes, return a list of PatientIds where every patient has every condition in the list.
In that case, the easiest thing to do is group your IcdPatients table by Id, so that we can tell every condition that a patient has by looking once. Then we check that every ConditionCode we're looking for is in the group. In code, that looks like:
var result = IcdPatient.GetIcdPatientList()
// group up all the objects with the same PatientId
.GroupBy(patient => patient.PatientId)
// gather the information we care about into a single object of type {int, List<int>}
.Select(patients => new {Id = patients.Key,
Conditions = patients.Select(p => p.ConditionCode)})
// get rid of the patients without every condition
.Where(conditionsByPatient =>
conditionsByPatient.Conditions.All(condition => filteringList.Contains(condition)))
.Select(conditionsByPatient => conditionsByPatient.Id);
In query format, that looks like:
var groupedInfo = from patient in IcdPatient.GetIcdPatientList()
group patient by patient.PatientId
into patients
select new { Id = patients.Key,
Conditions = patients.Select(patient => patient.ConditionCode) };
var resultAlt = from g in groupedInfo
where g.Conditions.All(condition => filteringList.Contains(condition))
select g.Id;
Edit: If you'd also like to let your user specify the ConditionName rather than the ConditionId then simply convert from one to the other, storing the result in filteringList, like so:
var conditionNames = // some list of names from the user
var filteringList = Icd.GetIcdList().Where(icd => conditionNames.Contains(icd.ConditionName))
.Select(icd => icd.ConditionCode);
I have a list of string tuples, say (P1,P2)
I'd like to know if there's a LINQ statement where I could group by P1 (in ascending order), and have that group contain all the P2 values for the group (in descending order).
For input: ("A","B"), ("A","C"), ("D","B")
I'd like to get two groups: "A" and "D" (in that order, every time)
where group "A" contains "C" and "B" (in that order, every time) and group "D" contains, well, "B".
Is this possible with the built-in LINQ classes or do I need to iterate the groups and sort them myself?
Nope, it's not hard - you just need to keep track of whether you're looking at a group or elements within the group. Here's a sample query:
var query = from tuple in tuples
orderby tuple.P1
group tuple.P2 by tuple.P1 into g
select new { Group = g.Key,
Elements = g.OrderByDescending(p2 => p2) };
Here's a complete example (avoiding .NET 4's Tuple type just for simplicity if you're using .NET 3.5):
using System;
using System.Collections.Generic;
using System.Linq;
public class MyTuple
{
public string P1 { get; set; }
public string P2 { get; set; }
}
class Test
{
static void Main()
{
List<MyTuple> tuples = new List<MyTuple>
{
new MyTuple { P1 = "A", P2 = "B" },
new MyTuple { P1 = "A", P2 = "C" },
new MyTuple { P1 = "D", P2 = "B" },
};
var query = from tuple in tuples
orderby tuple.P1
group tuple.P2 by tuple.P1 into g
select new { Group = g.Key,
Elements = g.OrderByDescending(p2 => p2) };
foreach (var group in query)
{
Console.WriteLine("{0}:", group.Group);
foreach (var value in group.Elements)
{
Console.WriteLine(" {0}", value);
}
}
}
}
Note that it can be slightly simpler if you're happy to do the ordering on a "need to know" basis:
var query = from tuple in tuples
orderby tuple.P1
group tuple.P2 by tuple.P1;
foreach (var group in query)
{
Console.WriteLine("{0}:", group.Key);
foreach (var value in group.OrderByDescending(x => x))
{
Console.WriteLine(" {0}", value);
}
}
List<Tuple<string, string>> tuples = //
var grouped = tuples.GroupBy(t => t.First)
.OrderBy(grp => grp.Key)
.Select(grp => new { Key = grp.Key, Items = grp.OrderBy(t => t.Second) });