从IQueryable查询返回一条记录

时间:2016-08-12 07:24:40

标签: c# entity-framework iqueryable

大家好我有这个问题

  public IQueryable<HeaderMRC> ShowHeader(int MRCId)
        {
            return from m in _ctx.MaterialRequestContractorDetails
                   where m.MaterialRequestContractorId == MRCId
                   join materialRequestContractor in _ctx.MaterialRequestContractors on m.MaterialRequestContractorId equals materialRequestContractor.Id

                   join mat in _ctx.MaterialDescriptions on m.MaterialDescriptionId equals mat.Id
                   join l in _ctx.Lines on m.LineId equals l.Id
                   join s in _ctx.Sheets on l.Id equals s.LineId
                   select new HeaderMRC()
                   {
                       Code = materialRequestContractor.Code,
                       UnitArea = l.Unit,
                       LineType = l.Type,
                       RequestDate = materialRequestContractor.RequestDate
                   };

        }

此查询生成多条记录,但我只需要第一条记录,如何将第一个值作为IQueryable返回

2 个答案:

答案 0 :(得分:0)

只需添加.First().FirstOrDefault()

即可
return (from m in _ctx.MaterialRequestContractorDetails
               where m.MaterialRequestContractorId == MRCId
               join materialRequestContractor in _ctx.MaterialRequestContractors on m.MaterialRequestContractorId equals materialRequestContractor.Id

               join mat in _ctx.MaterialDescriptions on m.MaterialDescriptionId equals mat.Id
               join l in _ctx.Lines on m.LineId equals l.Id
               join s in _ctx.Sheets on l.Id equals s.LineId
               select new HeaderMRC()
               {
                   Code = materialRequestContractor.Code,
                   UnitArea = l.Unit,
                   LineType = l.Type,
                   RequestDate = materialRequestContractor.RequestDate
               }).FirstOrDefault();

OR

return (from m in _ctx.MaterialRequestContractorDetails
               where m.MaterialRequestContractorId == MRCId
               join materialRequestContractor in _ctx.MaterialRequestContractors on m.MaterialRequestContractorId equals materialRequestContractor.Id

               join mat in _ctx.MaterialDescriptions on m.MaterialDescriptionId equals mat.Id
               join l in _ctx.Lines on m.LineId equals l.Id
               join s in _ctx.Sheets on l.Id equals s.LineId
               select new HeaderMRC()
               {
                   Code = materialRequestContractor.Code,
                   UnitArea = l.Unit,
                   LineType = l.Type,
                   RequestDate = materialRequestContractor.RequestDate
               }).First();

FirstOrDefault将返回第一个,或者如果没有一个Default(T),如果它是一个类对象,则通常为null ...

所以要把它作为一个IQueryable而不是我喜欢的事情:

return new List<HeaderMRC> { (from m in _ctx.MaterialRequestContractorDetails
               where m.MaterialRequestContractorId == MRCId
               join materialRequestContractor in _ctx.MaterialRequestContractors on m.MaterialRequestContractorId equals materialRequestContractor.Id

               join mat in _ctx.MaterialDescriptions on m.MaterialDescriptionId equals mat.Id
               join l in _ctx.Lines on m.LineId equals l.Id
               join s in _ctx.Sheets on l.Id equals s.LineId
               select new HeaderMRC()
               {
                   Code = materialRequestContractor.Code,
                   UnitArea = l.Unit,
                   LineType = l.Type,
                   RequestDate = materialRequestContractor.RequestDate
               }).FirstOrDefault() }.AsQueryable();

答案 1 :(得分:0)

只需将您的查询与()括起来,然后追加Take(1),这是FirstOrDefault的等效设置:

return (from m in ...
        ...
        ...).Take(1);