score:2
Let's assume that you have the classes set up with associations, such as (using CodeFirst EF). If using the designer, then use the associations and classes as you've defined them.
public class MemberToMembership
{
[Key] // maybe also DatabaseGenerated.Identity?
public virtual int Id { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual decimal JoinFee { get; set; }
public virtual decimal ChargePerPeriod { get; set; }
public virtual decimal InductionFee { get; set; }
public virtual int OptionId { get; set; }
[ForeignKey("OptionId")]
public virtual MembershipOption Option { get; set; }
}
public class MembershipOption
{
[Key]
public virtual int Id { get; set; }
public virtual string Period { get; set; }
public virtual int TypeId { get; set; }
[ForeignKey("TypeId")]
public virtual MembershipType Type { get; set; }
public virtual ICollection<MemberToMembership> MemberMap { get; set; }
}
public class MembershipType
{
[Key]
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<MembershipOption> Options { get; set; }
}
Now we can take advantage of the relationships to help form the query.
var dateFrom = new DateTime(2010, 9, 8); // start of day we care about
var dateTo = new DateTime(2011, 9, 6).AddDays(1); // end of day we care about
var query = tgsdbcontext.MemberToMemberships
.Where( mm => mm.StartDate > dateFrom && mm.StartDate < dateTo )
.GroupBy( mm => mm.Option.Type.Name )
.Select( g => new
{
Period = g.Key,
Count = g.Count(),
Value = g.Sum( e => e.JoinFee
+ e.InductionFee
+ (e.Option.Period == "year"
? EntityFunctions.DiffYears(e.StartDate,e.EndDate) * e.ChargePerPeriod
: EntityFunctions.DiffMonths(e.StartDate,e.EndDate) * e.ChargePerPeriod))
});
score:1
Try something like this:
DateTime dateFrom = new DateTime(2010, 9, 8);
DateTime dateTo = new DateTime(2001, 9, 6);
var query = from t1 in tsgdbcontext.membertomship
join t2 in tsgdbcontext.mshipoptions on t1.mshipOption_Id equals t2.mshipOption_Id
join t3 in tsgdbcontext.mshiptypes on t1.mshipType_Id equals t3.mshipType_Id
where t1.memberToMship_StartDate >= dateFrom &&
t1.memberToMship_StartDate <= dateTo
group t1 by t1.mshipType_Name into g
select new {
mshipType_Name = g.Key,
mshipssold = g.Count(),
value = (from x in g select memberToMship_InductionFee +
memberToMship_JoinFee + ((mshipOption_Period = 'year' ?
memberToMship_EndDate.Year - memberToMship_StartDate.Year :
ConvertTimeSpanToMonths(memberToMship_EndDate -
memberToMship_StartDate)) * memberToMship_ChargePerPeriod)
).Sum()
}
NOTE: The code above is untested so might need tweaking, but this should give you the general idea of how to do this
You'll need to write or copy one of the available solutions for ConvertTimeSpanToMonths
method to convert a TimeSpan to months. Here a link to something you can use: Date Subtraction Examples.
Source: stackoverflow.com
Related Articles
- how can i get the membership sold count
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- LINQ Source Code Available
- prevent unnecessary cross joins in count query of generated sql code
- .NET 4 Code Contracts: "requires unproven: source != null"
- creating Linq to sqlite dbml from DbLinq source code
- source code for LINQ 101 samples
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- LINQ with groupby and count
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- LINQ - Left Join, Group By, and Count
- Value cannot be null. Parameter name: source
- Linq: GroupBy, Sum and Count
- Linq with group by having count
- Lists: Count vs Count()
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Count property vs Count() method?
- Roslyn failed to compile code
- How to Count Duplicates in List with LINQ
- Using GroupBy, Count and Sum in LINQ Lambda Expressions
- Linq distinct - Count
- Can I have an incrementing count variable in LINQ?
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- Best practise in splitting up query result by a column
- linq group data
- linq-to-sql is this the most efficient code review
- How to assign linq anonymous type variable
- Loop an array and send the result to view ASP.NET
- c# linq to xml XElement group by ancestor attribute to create list of entities collection with key
- How to do "nested" eager loading in Entity Framework?
- C# notation understanding Select(int.Parse)
- Create generic Function to filter many Objects that have the same Base Class
- Enumerable.Sum() overflowing
- How to group a list of data into year, season, month, week, day?
- Combining The Results Of Two Linq Queries Into A Single Var?
- Linq Query For Many to Many Relation within Intersecting Table
- LINQ to SQL: Selecting a one-to-many relationship
- Linq to XML, parsing with XNameSpace
- Adding array title to Linq-to-SQL-to-JSON in Web API
- Parsing xml data using Linq to Xml
- Without using a html parser is there a way, using an ordinary string method to get the part of a string between two specified strings, in my case tags
- LINQ to add continuous records based on a flag to JSON
- Fill empty spaces in List of custom type