Is it possible to order the results of a LINQ join operation based on the inner collection order?
Say I have two collections:
var names = new[]{"John", "Mary", "David"};
var persons= new[]{ new Person{Name="John", Title"Prof"}, new Person{Name="Mary", Title="Accountant"}, new Person{Name="David", Title="Mechanic"}, new Person{Name="Peter", Title="Homeless"}}
if I do a LINQ join to get a subset of persons as follows:
var taxPayers =
persons
.Join(names , p => p.Name, n => n, (p, n) => p)
.Select(f => new KeyValuePair<string, object>(f.Name, f.Title));
The result is ordered based on the persons array.
It is possible using LINQ, to order taxPayers based on the order of names? Or is this not a LINQable operation?
TIA.
Simply reversing the join should work. Instead of joining names to persons, join persons to names.
Related
I Would like to build the dynamic Linq to Sql query for groupjoin clause where groupjoin will include multiple dynamic columns for joining.
I have looked the below artile for left join but it do not have the facility for multiple columns to join:
How do I do a left outer join with Dynamic Linq?
Below is the query at compile time which i need to acheive dynamically:
`var source = lParent.GroupJoin(lChild,
p => new{ p.PID,p.CategoryId (These are dynamic columns)}
c => new{ c.PID,c.CategoryId (These are dynamic columns)}
(p, g) => new { Parent = p, Childs= g })
.SelectMany( p => p.Childs.DefaultIfEmpty(),
(p,g) => new { Parent=p.Parent, Childs=g});`
Thankyou.
Ashutosh.
I have two tables..
Student (StudentId,Name,FatherName)
Qualification (QualificationId,StudentId,DegreeName)
I have got data like this..
var myList = (from c in entities.Students
join q in entities.Qualifications on c.StudentId equals q.StudentId
select new {c.Name,c.FatherName,q.DegreeName}).ToList();
Now i want to filter myList more.. How can i do it, like..
var filteredList = myList.Select(c=> new Student
{
Name=c.Name,
FatherName=c.FatherName
//Degree=C.Degree
}).ToList();
The above Linq Query is not working if i want to get DegreeName also, My Question is how to further Filter myList.
Thanks.
var filteredList = myList.Where(i => i.FatherName == "Shahid").ToList();
Keep in mind since you called ToList() on the original query you are now filtering in memory. If you want to filter in the database then remove the ToList() on the first query and do it like this:
var myList = from c in entities.Students
join q in entities.Qualifications on c.StudentId equals q.StudentId
select new {
c.Name,
c.FatherName,
q.DegreeName
};
var filteredInDatabase = myList.Where(i => i.FatherName == "Shahid").ToList();
I am trying to get from 3 related tables by using LINQ. But when I use 2 joins, the result takes only elements getting from 2nd join. Here is my code:
var myAssList = mldb.Assigns
.Join(mldb.Lists,
a => a.list_id,
l => l.id,
(a, l) => new {
Assign = a,
List = l
})
.Where(a => a.Assign.assigned_to == "myname")
.Join(mldb.Elements,
li => li.List.id,
e => e.parent_server_id,
(li, e) => new {
Element = e
});
var jsonSerialiser = new JavaScriptSerializer();
var listListJson = jsonSerialiser.Serialize(myAssList);
this Json return only attributes from Element(e) and List(li). But I want to get also the attributes from Assign(a).
The SQL query I am trying to realize in LINQ is that:
select * from Assigns
inner join Lists
on Assigns.server_list_id=Lists.id
inner join Elements
on Lists.id=Elements.parent_id
where Assigns.assigned_to='myname'
So, how can I get the attributes from the first join also (from "a", "l" and "e")?
You can access Assign entity from outer sequence li variable:
.Join(mldb.Elements,
li => li.List.id,
e => e.parent_server_id,
(li, e) => new {
Element = e,
Assign = li.Assign // here
});
from a in mldb.Assigns
join l in mldb.Lists on a.list_id equals l.id
join e in mldb.Elements on l.id equals e.parent_server_id
where a => a.Assign.assigned_to == "myname"
select new { Assign = a, Element = e }
This is so called "query syntax". It makes LINQ expressions looks like SQL queries.
In the end they are translated to IEnumerable extension methods. If you want
to join multiple tables then query syntax is more readable. Another useful feature
of query syntax is let clause. With the aid of it, you can declare additional variables
inside your queries.
I want to do a group join with the following three tables using linq extension methods
Custumer
Order
OrderShippingInfo
Custumer can have many Orders and each order can have only one OrderShippingInfo.
So my goal is do a group join of these 3 tables and return a anonymous object that has one Customer and an array of {Order, OrderShippinfo}.
I know how to do a groupjoin between customer and order but I don't know how to add the OrderShippInfo in the collection
Customer.GroupJoin(Order, c=>c.customerID, o=>o.CustomerID, (c,o)=> new {c,o})
Thanks a lot
How about that:
Customer.GroupJoin(Order.Join(OrderShippinInfo,
o=>o.OrderID,
s=>s.OrderID,
(o, s) => new { Order = o, ShippingInfo = s}),
c=>c.customerID,
oi=>oi.Order.CustomerID,
(c,oi)=> new {c,oi})
I have a LINQ statement I am trying to get right, so maybe going about this all wrong. My objective is to query a table and join in another table to get counts.
Places
ID, Display
ProfilePlaces
ID, PlaceID, Talk, Hear
Basically Places have ProfilePlaces in a one to many relationship. I want to get the number SUM of ProfilePlaces that have Talkand Hear. Talkand Hear are bit fields.
The following gives me a unique list of Places, so I need to add in the Talkand Hear counts.
var counts = from p in db.Places
join pp in db.ProfilePlaces on p.ID equals pp.PlaceID
group new { Place = p } by p.Display;
I thought something like this, but not having any luck
var counts = from p in db.Places
join pp in db.ProfilePlaces on p.ID equals pp.PlaceID
group new { Place = p,
Talk = pp.Count(t => t.Talk == true),
Hear = pp.Count(t => t.Hear == true)
} by p.Display;
Thanks for any help.
You want to do a GROUP JOIN to get the counts for each Place.
var counts2 = from p in places
join pp in profilePlaces on p.ID equals pp.PlaceID into g
select new
{
Place = p,
CountMeet = g.Count(a => a.Meet),
CountTalk = g.Count(a => a.Talk)
};
Here's the documentation on the different joins from MSDN:
http://msdn.microsoft.com/en-us/library/bb311040.aspx