简体   繁体   English

如何优化 linq 查询 c# 复杂 object?

[英]how to optimize a linq query c# complex object?

I am making a method that returns me the visitor's data, make this query with linq c #, which is the following,我正在制作一个返回访问者数据的方法,使用 linq c # 进行此查询,如下所示,

public async Task<Models.Visitor> GetVisitorByEmail(string email)
{
    return (from v in _unitOfWorkModel.Repository<Visitor>().GetList()
            where v.Email.ToLower() == email.ToLower()
            select new Models.Visitor
            {
                VisitorId = v.VisitorId,
                FirstName = v.FirstName,
                SecondName = v.SecondName,
                FirstLastName = v.FirstLastName,
                SecondLastName = v.SecondLastName,
                Gender = (from g in _unitOfWorkModel.Repository<Gender>().GetList()
                          where g.GenderId == v.GenderId
                          select new Models.Gender
                          {
                              GenderId = g.GenderId,
                              GenderName = g.GenderName,
                          }).FirstOrDefault(),
                Email = v.Email,
                Telephone = v.Telephone,
                Family = (from vf in _unitOfWorkModel.Repository<VisitorFamily>().GetList()
                          join f in _unitOfWorkModel.Repository<Family>().GetList()
                          on vf.FamilyId equals f.FamilyId
                          where vf.VisitorId == v.VisitorId
                          select new Models.Family
                          {
                              FamilyId = f.FamilyId,
                              FamilyName = f.FamilyName,
                              PhotoPath = f.PhotoPath
                          }).ToList(),
                Territory = (from vt in _unitOfWorkModel.Repository<VisitorTerritory>().GetList()
                             join t in _unitOfWorkModel.Repository<Territory>().GetList()
                             on vt.TerritoryId equals t.TerritoryId
                             where vt.VisitorId == v.VisitorId
                             select new Models.Territory
                             {
                                 TerritoryId = t.TerritoryId,
                                 TerritoryName = t.TerritoryName,
                                 Axis = (from a in _unitOfWorkModel.Repository<Axis>().GetList()
                                         where a.AxisId == t.AxisId
                                         select new Models.Axis
                                         {
                                             AxisId = a.AxisId,
                                             AxisName = a.AxisName,
                                         }).FirstOrDefault(),
                             }).ToList(),
                IsActive = v.IsActive,
                PhotoPath = v.PhotoPath,
                UserTypeId = v.UserTypeId,
                UserId = v.UserId,
                CreateBy = v.CreateBy,
                CreationDate = v.CreationDate,
                ModifiedBy = v.ModifiedBy,
                ModifiedDate = v.ModifiedDate,
            }).FirstOrDefault();
}

I would like to know if I have 20 visitors for example, if the api that I am developing will give me good response times or is there another more optimal way to do it, since I am new to creating complex objects with linq c #, I am using ASP.NET CORE 3.1 and Core Entity Framework,例如,我想知道我是否有 20 个访问者,如果我正在开发的 api 会给我良好的响应时间,或者是否有另一种更优化的方法来做到这一点,因为我不熟悉使用 linq Z4A8A036F09D347B737 创建复杂对象我正在使用 ASP.NET CORE 3.1 和核心实体框架,

I would appreciate the help to optimize this query you made.我将不胜感激帮助您优化此查询。

I am attentive to your suggestions我很注意你的建议

Here is the example of style in which you must attach all nested entities:这是您必须附加所有嵌套实体的样式示例:

public Task<IQueryable<Models.Gender>> GetGenderById(int id)
{
    return _unitOfWorkModel.Repository.Get<Gender>(x => x.GenderId = id).
}

In this example you're passing query on SQL server and return result in one record by ToList() (in GetVisitorByEmail() method).在此示例中,您将在 SQL 服务器上传递查询,并通过ToList() (在GetVisitorByEmail()方法中)在一条记录中返回结果。

Get() method must be in your base repository by default and all yours repos must inherit it with passing it's entity type to:默认情况下, Get()方法必须在您的基础存储库中,并且您的所有存储库都必须继承它,并将其实体类型传递给:

public abstract class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        protected readonly ApplicationDbContext DbContext;
        protected readonly DbSet<T> DbSetEntity;
        private readonly ILogger<BaseRepository<T>> _logger;

        protected BaseRepository(ApplicationDbContext dbContext,ILoggerFactory loggerFactory)
        {
            DbContext = dbContext;
            DbSetEntity = DbContext.Set<T>();

            _logger = loggerFactory.CreateLogger<BaseRepository<T>>();
        }

        ...

        public virtual T Get(Expression<Func<T, bool>> predicate)
        {
            return DbContext.Set<T>().FirstOrDefault(predicate);
        }

And use this method:并使用此方法:

return (from v in _unitOfWorkModel.Repository<Visitor>
            where v.Email.ToLower() == email.ToLower()
            select new Models.Visitor
            {
                VisitorId = v.VisitorId,
                FirstName = v.FirstName,
                SecondName = v.SecondName,
                FirstLastName = v.FirstLastName,
                SecondLastName = v.SecondLastName,
                Gender = GetGenderById(v.GenderId).ToList(),
                Email = v.Email
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM