I have a problem loading the correct data to a DTO using EF and linq.
From my DB I receive following example data:
1, 1, 1
1, 1, 2
1, 1, 3
2, 1, 4
2, 1, 5
etc.
I want to load these data in a DTO which should look like this:
int, int, ICollection<int>
so for the example data:
new MyDto(1, 1, new List<int> { 1, 2, 3 });
new MyDto(2, 1, new List<int> { 4, 5 });
This is my linq query
var result = (from adresses in context.Adress
join person in context.Person on adresses.PersonId equals person.Id
select new MyObj { Id1 = adresses.Id1, Id2 = adresses.Id2, PersonId = person.Id })
But it is wrong, since it doesn't group by Id1 and Id2 and doesn't put the personIds in the list...
Could you please tell me how I can achieve this?
Pivot data using Linq is a better way. You can take look at this link:
Is it possible to Pivot data using LINQ
To answer your question, below is an example:
var result = (from adresses in context.Adress
join person in context.Person on adresses.PersonId equals person.Id
group address by address.Id1 into gResult
select new{
Id1 = gResult.Key,
Id2 = gResult.Select(r => r.Id2).FirstOrDefault (),
Id3 = gResult.Select (r => r.Id3)
});
In your Address class, do you have a property for a Person instance so you're able to set up a relationship between the two classes? If so, the following query may get you the result set that you're looking for:
public class Address
{
public int Id1 { get; set; }
public int Id2 { get; set; }
public virtual Person Person { get; set; }
}
public void Foo()
{
IEnumerable<MyObj> = context.Address.Select(x => new {
Id1 = x.Id1,
Id2 = x.Id2,
PersonId = x.Person.Id
});
}
Thanks for the good answers of you guys, I could finally work it out :-)
var result = from tuple in (from address in context.Adresses
join person in context.Persons on address.PersonId equals person.Id
select new { person.Id, address.Id1, address.Id2})
group tuple by new { tuple.Id1, tuple.Id2 } into myGrouping
select
new MyObj
{
Id1 = myGrouping.Key.Id1,
Id2 = myGrouping.Key.Id2,
PersonIds = myGrouping.Select(x => x.PersonId).Distinct()
};
Related
I have two collection with more number of items like a million. I want to compare them to find out the below in C#
how to find the matching items between Orders1 and Orders2
how to find the Orders1 which are in Orders2
how to find the Orders2 which are in Orders1
how to find the Orders1 which are not in Orders2
how to find the Orders2 which are not in Orders1
public class Order
{
public int OrderID { get; set; }
public string OrderDate { get; set; }
public string StoreID { get; set; }
public float TotalPrice { get; set; }
}
static void Main(string[] args)
{
List<Order> Orders1 = new List<Order>();
Orders1.Add(new Order { OrderID = 1, StoreID = "A01", TotalPrice = 12.1F });
Orders1.Add(new Order { OrderID = 2, StoreID = "A02", TotalPrice = 12.2F });
Orders1.Add(new Order { OrderID = 3, StoreID = "A03", TotalPrice = 12.3F });
Orders1.Add(new Order { OrderID = 4, StoreID = "A04", TotalPrice = 12.4F });
Orders1.Add(new Order { OrderID = 1, StoreID = "A01", TotalPrice = 12.1F });
Orders1.Add(new Order { OrderID = 5, StoreID = "A05", TotalPrice = 12.5F });
List<Order> Orders2 = new List<Order>();
Orders2.Add(new Order { OrderID = 1, StoreID = "A01", TotalPrice = 12.1F });
Orders2.Add(new Order { OrderID = 2, StoreID = "A02", TotalPrice = 12.2F });
Orders2.Add(new Order { OrderID = 2, StoreID = "A03", TotalPrice = 12.2F });
Orders2.Add(new Order { OrderID = 1, StoreID = "A01", TotalPrice = 12.1F });
Orders2.Add(new Order { OrderID = 5, StoreID = "A05", TotalPrice = 12.5F });
Orders2.Add(new Order { OrderID = 6, StoreID = "A06", TotalPrice = 12.6F });
Orders2.Add(new Order { OrderID = 7, StoreID = "A07", TotalPrice = 12.7F });
}
Is performance important? Please keep in mind that you can check all this in O(mn), where m and n are the sizes of Orders1 and Orders2, respectively.
If performance is not that important, then go for a simple 2 cycles approach:
Have 5 lists (one per each one of your cases), and classify your orders accordingly.
If performance IS an important factor, then:
Learn how to implement IComparable, as you need to compare three things: OrderID, StoreID and TotalPrice. You need to implement it in your Order class. Google is your friend. Here is an example: https://www.geeksforgeeks.org/c-sharp-program-to-implement-icomparable-interface/#:~:text=C%23%20provides%20an%20IComparable%20interface,on%20which%20basis%20to%20sort.
Then you need to create your own List class, and insert using binary search. Then when you scroll throughout the lists, your searches will be faster.
In both scenarios, you should (this is not a must if you have enough memory) sort both lists by some criteria (probably OrderID, again IComparable could help), and when you classify the orders, delete them both Order lists, so you don't end up having 2 lists of millions + 5 lists of classified orders. Again, learn memory management.
Based on my personal experience, don't go for lambda expressions. If you claim you have more than a million entries in each list, you might run into memory issues.
My personal suggestion: Learn to implement IComparable and create your own insert. I think the lesson of this exercise is to learn how to attack this type of problems when you have millions of rows of data.
Take a look at C# join clause. contains full documentation of how to implement
Inner join
Group join
Left outer join
and then manipulate your queries with Join to extract the data.
var intersecting = from o1 in Orders1
join o2 in Orders2 on o1.OrderID equals o2.OrderID
select o1;
var result = from x in Orders1
join y in Orders2 on x.OrderID equals y.OrderID
select x; //1
Orders1.Where(x=>Orders2.Any(y=>y.OrderID == x.OrderID )); //2
Orders2.Where(x=>Orders1.Any(y=>y.OrderID == x.OrderID )); //3
Orders1.Where(p => Orders2.All(p2 => p2.OrderID != p.OrderID)); //4
Orders2.Where(p => Orders1.All(p2 => p2.OrderID != p.OrderID)); //5
I'm struggling with linq (left join - group - count). Please help me.
Below is my code and it gives me this result.
Geography 2
Economy 1
Biology 1
I'm expecting this...
Geography 2
Economy 1
Biology 0
How can I fix it?
class Department
{
public int DNO { get; set; }
public string DeptName { get; set; }
}
class Student
{
public string Name { get; set; }
public int DNO { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Department> departments = new List<Department>
{
new Department {DNO=1, DeptName="Geography"},
new Department {DNO=2, DeptName="Economy"},
new Department {DNO=3, DeptName="Biology"}
};
List<Student> students = new List<Student>
{
new Student {Name="Peter", DNO=2},
new Student {Name="Paul", DNO=1},
new Student {Name="Mary", DNO=1},
};
var query = from dp in departments
join st in students on dp.DNO equals st.DNO into gst
from st2 in gst.DefaultIfEmpty()
group st2 by dp.DeptName into g
select new
{
DName = g.Key,
Count = g.Count()
};
foreach (var st in query)
{
Console.WriteLine("{0} \t{1}", st.DName, st.Count);
}
}
}
var query =
from department in departments
join student in students on department.DNO equals student.DNO into gst
select new
{
DepartmentName = department.DeptName,
Count = gst.Count()
};
I don't think any grouping is required for answering your question.
You only want to know 2 things:
- name of department
- number of students per department
By using the 'join' and 'into' you're putting the results of the join in the temp identifier gst. You only have to count the number of results in the gst.
var query = from dp in departments
from st in students.Where(stud => stud.DNO == dp.DNO).DefaultIfEmpty()
group st by dp.DeptName into g
select new
{
DName = g.Key,
Count = g.Count(x => x!=null)
};
You want to group the students by the department name but you want the count to filter out null students. I did change the join syntax slightly although that really does not matter to much.
Here is a working fiddle
Well, see what #Danny said in his answer, it's the best and cleanest fix for this case. By the way, you could also rewrite it to the lambda syntax:
var query = departments.GroupJoin(students,
dp => dp.DNO, st => st.DNO,
(dept,studs) => new
{
DName = dept.DNO,
Count = studs.Count()
});
I find this syntax much more predictable in results, and often, shorter.
BTW: .GroupJoin is effectively a "left join", and .Join is "inner join". Be careful to not mistake one for another.
And my answer is similar to #Igor
var query = from dp in departments
join st in students on dp.DNO equals st.DNO into gst
from st2 in gst.DefaultIfEmpty()
group st2 by dp.DeptName into g
select new
{
DName = g.Key,
Count = g.Count(std => std != null)
};
g.Count(std => std != null) is only one change you should take.
I have a list of codes as follows
public Code{
int id;
string Description;
}
List<Code> AllCodes;
I have a list of selected codes from a different source.
var relatedCodes = //gets the list of int 'id's from a different source.
Using linq, I need to join AllCodes and relatedCodes so that the resultant list contains all the Code elements of the given ids. It is known that all the int values in relatedCodes are valid ids in AllCodes. [relatedCodes is an int array]
result = //how to write the linq expression?
I was trying something like this but it throws error
result = AllCodes.All(x => x.Code==relatedCodes);
First of all there is nothing to do with Join. Question is briefly How can I get the Codes of which relatedCodes contains the id?. You can use Where to filter your list.
var result = AllCodes.Where( c=> relatedCodes.Contains(c.id));
List<Code> result = AllCodes.Where(x => relatedCodes.Contains(x.id)).ToList();
EDIT:
Since relatedCodes is of type int[] (I used an array of type Code) the solution looks slightly different, but not by too much:
var relatedCodes = new int[2] { 2, 4 };
var joinedCodes = from ac in AllCodes
join rc in relatedCodes on ac.Id equals rc
select ac;
ORIGINAL answer
One possibility is to use join:
void Main()
{
var AllCodes = new List<Code>()
{
new Code() {Id = 1, Description="Foo1"},
new Code() {Id = 2, Description="Bar2"},
new Code() {Id = 3, Description="Foo3"},
new Code() {Id = 4, Description="Bar4"}
};
var relatedCodes = new Code[2]
{
new Code() {Id = 2, Description="Bar2"},
new Code() {Id = 4, Description="Bar4"}
};
var joinedCodes = from ac in AllCodes
join rc in relatedCodes on ac.Id equals rc.Id
select ac;
joinedCodes.Dump();
}
// Define other methods and classes here
public class Code{
public int Id { get; set; }
public string Description { get; set; }
}
Ouput:
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 want to show OrderDetails Count near the orders information in the grid but in the select Unit i can only select the Key and Count. What is the way to select the orders information?
var Q = from Data in Context.Orders
join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID
group Data by Data.OrderID into grouped
select new
{
grouped=g.Key,
Count = grouped.Count()
};
You can group it by whole order entity like
var Q = from Data in Context.Orders
join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID
group Data by Data into grouped
select new
{
OrderId = grouped.Key.OrderId,
OrderDate = grouped.Key.OrderDate
Shipping = grouped.Key.Shipping
.
.
.
Count = grouped.Count()
};
EDIT Linqpad program for similar query on in memory collection of objects
void Main()
{
var orders = new List<Order>{
new Order{OrderId = 1, DeliverIn = 5},
new Order{OrderId = 2, DeliverIn = 6},
new Order{OrderId = 3, DeliverIn = 5},
};
var lines = new List<OrderLine>{
new OrderLine{LineId = 1, OrderId = 1, ProductId = 1},
new OrderLine{LineId = 2, OrderId = 1, ProductId = 2},
new OrderLine{LineId = 3, OrderId = 1, ProductId = 3},
new OrderLine{LineId = 4, OrderId = 2, ProductId = 1},
new OrderLine{LineId = 5, OrderId = 2, ProductId = 3},
new OrderLine{LineId = 6, OrderId = 2, ProductId = 4},
};
var query = from o in orders join l in lines on
o.OrderId equals l.OrderId
group o by o into grouped
select new
{
Count = grouped.Count(),
grouped.Key.OrderId,
grouped.Key.DeliverIn
};
Console.WriteLine(query);
}
// Define other methods and classes here
public class Order
{
public int OrderId{get;set;}
public int DeliverIn{get;set;}
}
public class OrderLine
{
public int LineId{get;set;}
public int OrderId{get;set;}
public int ProductId{get;set;}
}
and if you don't have linq pad simply go and grab it from their site. It is simply awesome.
Check out IGrouping documentation on MSDN.
public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>,
IEnumerable
Pay attention to IEnumerable. Count is just an extension method of IEnumerable. You can easily Select from grouping or loop through it.
For example:
var Q = from Data in Context.Orders
join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID
group Data by Data.OrderID into grouped
select new
{
grouped=g.Key,
Count = grouped.Count(),
Orders = grouped.ToArray()
//you can also just return grouped itself to support lazy queries
};
Just flatten them into array or list and then get its count.
select new
{
Key = g.Key,
Orders = grouped.ToArray()
};
Then get count:
int count = result.Orders.Count; // Property of an array.