答:我们公司的ASP.NET 笔试题,你觉得难度如何

闲来无事,逛逛园子,发现有个面试题,觉得有意思。已自己的理解答来看看,不足之处,请多指教。

原文地址: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得到右方的查询结果。

191437386251620

分析图片结果,可知,是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>
该表单同步提交的时候,如何修改以上HTML和路由配置以使该请求进入下方的action中:
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#基础的应用。就当学习巩固知识了,好久没写,有些语法感觉手生,但是思路还在,随便写写, 不足之处请多指教。

原文地址:https://www.cnblogs.com/lipanpan/p/4118633.html