score:1
Accepted answer
i would create a class that will hold gather all "store entities" and their sales, i think that using a collection of this class (storeentity
in my example) it will be much easier to perform a variety of data manipulation (for this task and in future tasks).
here is my suggestion:
public class storeentity
{
person personentity { get; set; }
customer customerentity { get; set; }
list<sale> saleslist { get; set; }
public storeentity(person p, customer c,list<sale> sales)
{
this.personentity = p;
this.customerentity = c;
this.saleslist = sales;
}
public int salescount
{
get
{
if (saleslist == null)
{
return 0;
}
else
{
return saleslist.count;
}
}
}
}
public list<storeentity> entities { get; set; }
public void mostorders()
{
list<person> per = data.getallpersons();
list<sale> sale = data.getallsales();
list<customer> cus = data.getallcustomers();
entities = new list<storeentity>();
foreach (person p in per)
{
var cust = cus.where(x => x.personid == p.personid).select(x => x).singleordefault();
if (cust != null)
{
var sales = sale.where(x => x.customerid == cust.customerid).tolist();
storeentity entity = new storeentity(p, cust, sales);
entities.add(entity);
}
}
// now you have list of all entities with their sales
// it will be musch easy to manipulate with linq
// you can do this:
entities = entities.orderby(x => x.salescount).tolist();
// or this...
var bestentity = entities.max(x => x.salescount);
}
Source: stackoverflow.com
Related Query
- Best way to join 3 lists and count how many times an ID is in the list using linq
- How to join two tables using Linq and count the rows of one column matched records
- How can I code an outer join using LINQ and EF6?
- Get the count of distinct elements from a list of lists using LINQ
- How to find how many times the same object appears in a list and then find a property of the most appeared object
- How to join a list of lists in a unique list using Linq
- What is the best way to get the percentage for an object in linq list and map it to JSON?
- How to Join in LINQ using Lambda expressions and get the result in DTO?
- Count how many cells are null in a row using Linq and c#
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to get the distinct record name and its each record count to bind to gridview in C# using Linq from SQL Server
- How to Group and Sum List of Lists using Linq
- How to get lists of X and Y from a list of points using a single linq query?
- How to join a list and large lists/tables using LINQ
- Using LINQ. How can I compare two simple lists to give another list of objects in the new and not in the old list?
- How can I Using LinQ search in a list by the input and print those that contain the letters?
- What is the best way to perform a left/right outer join in LINQ and Entity Framework (method)?
- How to compare the dates and gets the day count using c# in LINQ
- How to group by with left join by multiple columns using linq and get count of grouped records?
- Getting Count of the first list from joined two lists using Linq C#
- What's the best way to construct this LINQ query using orderby and group by?
- How to get the list of employees and their reporting level recursively using LINQ
- How can I join a list of employees and genders using linq
- Is there a way to update a generic list using LINQ rather than deleting a member of the list and adding it back?
- How do I Count the number of parent groups using LINQ and child repo
- C# Linq query to join two lists and assign the result to new list
- Best way to compare two classes (Users and Holidays) and create a list of Users who have not created an instance of the Holiday class with LINQ
- Linq query to join 2 lists of objects and get the altered values as list
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
More Query from same tag
- Selecting distinct entity values based on string field name
- EF Code First: Methods that can translate to SQL
- Select 2 Column of 2 Table
- LINQ with 2 WHERE condition
- LINQ: add/sum all timespans in a collection together
- How to properly build a RavenDB spatial index, while also checking if any List<objects> match?
- Rearrange Table Data using Linq
- Non-recognized member in lambda expression of EF Core ThenInclude in Extension method
- Update existing row mysql linq
- LINQ OrderBy the first array AND update a second array holding indexes
- Cannot make 5 consecutive "joins" in LINQ?
- c# - Using LinQ to look for duplicate items in a list and updating object properties if so
- Dynamic values in linq query by other field and boolean value
- How do I use Like in Linq Query?
- Using my own method with LINQ to Entities
- Check if tie in count of lists using linq
- Entity Framework Linq SET QUERY_GOVERNOR_COST_LIMIT
- Select objects with unique ID's as another object type using LINQ
- C# Functions to accept arguments from XML
- How to split a list into 2 separate list based on a filed in the parent list with linq
- How to simplify repetitive OR condition in Where(e => e.prop1.contains() || e.prop2.contains() || ...)
- How to find Sum of DateTime using LINQ
- Linq XML add new parent
- Append index number to duplicated string value in a list - by using Lambda
- Need a LINQ statement - many to many relationship
- Entity Framework LINQ complex query - combine multiple predicates
- How to calculate STD dev and Average with linq and entity framework at the same time
- how do i union int? and int variables in linqpad
- How to choose columns from a query and display in datagrid
- How to read attribute value using linq to xml