请看原帖地址:(如有错误,请指出)
1-3题答案
public enum QuestionType { Text=0, MultipleChoice=1 } interface IQuestion { string Title { get; set; } QuestionType QueryType { get; } string GetAnswer(); } abstract class QuestionBase : IQuestion { private string title; public string Title { get { return title; } set { title = value; } } public string GetAnswer() { return "默认答案"; } public QuestionType QueryType { get { throw new NotImplementedException(); } } } class TextQuestion : QuestionBase { public QuestionType QueryType { get { return QuestionType.MultipleChoice; } } public string GetAnswer() { return "文本答案"; } } class MultipleChoiceQuestion : QuestionBase { public string GetAnswer() { return "单选答案"; } }
4,
public class Product { public string Name { get; set; } public string IsDeleted { get; set; } public List<Product> GetActiveProducts(IQueryable<Product> query) { return query.WhereNotDeleted().ToList(); } } public static class Class1 { /// <summary> /// 获取所有没有被删除的扩展方法 /// </summary> /// <param name="products"></param> /// <returns></returns> public static IQueryable<Product> WhereNotDeleted(this IQueryable<Product> products) { return products.Where(p => p.IsDeleted == "false"); } }
5,sql语句:
select name,[year],[month],sum(amout) as income from [user],income where [user].id = income.userid group by name,[year],[month]
6,Linq实现以上查询结果
public class User { public int Id { get; set; } public string Name { get; set; } } public class Income { public int Id { get; set; } public int UserId { get; set; } public decimal Amount { get; set; } public int Year { get; set; } public int Month { get; set; } } public class UserIncomeDto { public string Name { get; set; } public int Year { get; set; } public int Month { get; set; } public decimal Income { get; set; } } public class GetResult { public List<UserIncomeDto> GetUserIncomeDtos(IQueryable<User> users, IQueryable<Income> incomes) { var result = from income in incomes group income by new { income.UserId, income.Year, income.Month } into g join user in users on g.Key.UserId equals user.Id select new UserIncomeDto { Name = user.Name, Year = g.Key.Year, Month = g.Key.Month, Income = g.Sum(o => o.Amount) }; return result.ToList(); } }
8,
public static class VarDelegate { public static void Require<TSource>(this TSource tSource, Func<TSource, string> func, string exptionMsg) { if (string.IsNullOrEmpty(func(tSource))) { throw new Exception(exptionMsg); } } } public class Product { public string Name { get; set; } public string Description { get; set; } public void Validate1() { if (string.IsNullOrEmpty(this.Name)) { throw new Exception("please enter a name for the product"); } if (string.IsNullOrEmpty(this.Description)) { throw new Exception("product description is required"); } } public void Validate2() { this.Require(x => x.Name, "please enter a name for the product"); this.Require(x => x.Description, "product description is required"); } }
参考一下博客内容参考答案