閑來無事,逛逛園子,發現有個面試題,覺得有意思。已自己的理解答來看看,不足之處,請多指教。
原文地址:http://www.cnblogs.com/leotsai/p/aspnet-tests-for-juniors.html
第1題:請定義一個接口IQuestion,有【標題】和【問題種類】兩個屬性,其中【問題種類】是只讀的枚舉類型QuestionType,另外還有一個方法獲取該問題的答案(無參,返回字符串)。
public enum QuestionType { Text = 0, MultipleChoice = 1 } interface IQuestion { string Title { get; set; } QuestionType QuestionType { get; } string GetAnswer(); }
第2題:請定義一個抽象類QuestionBase,實現第一題中的IQuestion接口,其中【問題種類】屬性不在該抽象類中實現,而留在該抽象類的子類中實現;獲取答案的方法有默認實現,返回字符串“默認答案”。
public abstract class QuestionBase : IQuestion { public string Title { get; set; } public abstract QuestionType QuestionType { get; } public virtual string GetAnswer() { return "默認答案"; } }
第3題:請定義一個TextQuestion類,繼承自第2題中的QuestionBase;獲取答案的方法返回字符串”文本答案”。再定義一個MultipleChoiceQuestion類,繼承自第2題中的QuestionBase;獲取答案的方法返回字符串”單選答案”。
public class TextQuestion : QuestionBase { public override QuestionType QuestionType { get { return QuestionType.Text; } } public override string GetAnswer() { return "文本答案"; } } public class MultipleChoiceQuestion : QuestionBase { public override QuestionType QuestionType { get { return QuestionType.MultipleChoice; } } public override string GetAnswer() { return "單選答案"; } }
第4題:假設有實體類Product定義如下:
public class Product { public string Name { get; set; } public string IsDeleted { get; set; } }
現在有一個方法從IQueryable<Product>中獲取沒有刪除的Product列表,該方法實現如下:
public List<Product> GetActiveProducts(IQueryable<Product> query) { return query.WhereNotDeleted().ToList(); }
請完成擴展方法:WhereNotDeleted
static class ProductExtend { public static IQueryable<Product> WhereNotDeleted(this IQueryable<Product> product) { if (product == null || product.Count() < 1) { return null; } else { return product.Where(p => p.IsDeleted == "1"); } } }
第5題:假設數據庫中有User和Income兩張表如下,請仔細分析下方的示例數據,然后寫出SQL得到右方的查詢結果。
分析圖片結果,可知,是User,Incom的聯合查詢,並根據月份合並了Amount的數據而得。
select name,[year],[month],sum([amount]) as income from [user] left join [income] on [user].id=[income].userid group by name,[year],[month] order by name
第6題:根據第5題的數據結構,有如下兩個實體類和查詢結果類的定義:
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; } }
現有一個方法用LINQ的方式得到第5題的查詢結果,該方法定義如下:
public List<UserIncomeDto> GetUserIncomeDtos(IQueryable<User> users, IQueryable<Income> incomes) { throw new NotImplementedException(); }
請完成該方法的實現。
public List<UserIncomeDto> GetUserIncomeDtos(IQueryable<User> users, IQueryable<Income> incomes) { //linq 實現 var data = from o in ( from p in users join q in incomes on p.Id equals q.UserId select new { Name = p.Name, Year = q.Year, Month = q.Month, Income = q.Amount } ) group o by new { o.Name, o.Month, o.Year } into groupdata select new UserIncomeDto { Name = groupdata.Key.Name, Year = groupdata.Key.Year, Month = groupdata.Key.Month, Income = groupdata.Sum(p => p.Income) }; //泛型實現 var datal = users.GroupJoin(incomes, p => p.Id, q => q.UserId, (p, q) => new UserIncomeDto { Name = p.Name, Year = q.Sum(o => o.Year), Month = q.Sum(o => o.Month), Income = q.Sum(o => o.Amount), }); return data.ToList<UserIncomeDto>(); }
第7題:在ASP.NET MVC應用程序中,假設有如下HTML表單:
<form action="/admin/mobile/user/login"> <input type="text" placeholder="username"/> <input type="password" placeholder="password"/> <input type="submit" value="login"/> </form>
public class UserController : Controller { [HttpPost] public ActionResult Login(string username, string password) { throw new NotImplementedException(); } }
我所能想到的就是在html的form表單添加屬性:method="post"
<form action="/user/Login" method="post"> <input type="text" placeholder="username" name="username" /> <input type="password" placeholder="password" name="password" /> <input type="submit" value="login" /> </form>
第8題:請看如下代碼:
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"); } }
請完成Validate2方法中Require方法的定義和實現,從而使得Validate2與Validate1方法實現同樣的效果。
public void Require(Expression<Func<Product, string>> query, string msg) { Func<Product, string> func = query.Compile(); if (string.IsNullOrEmpty(func(this))) { throw new Exception(msg); } }
評價:這幾個題目,咋一看感覺不難,仔細一作覺得不簡單,平心而論,考的很實用,偏重於C#基礎的應用。就當學習鞏固知識了,好久沒寫,有些語法感覺手生,但是思路還在,隨便寫寫, 不足之處請多指教。