Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq store select子句作为函数变量_C#_Linq_Entity Framework Core_Asp.net Core Webapi - Fatal编程技术网

C# linq store select子句作为函数变量

C# linq store select子句作为函数变量,c#,linq,entity-framework-core,asp.net-core-webapi,C#,Linq,Entity Framework Core,Asp.net Core Webapi,有没有办法将linq语句的select子句存储为变量 我有下面的类,并且select子句是重复的。我可以以某种方式将它存储为变量,这样我就不需要重复它了吗 public class TractRepository : ITractRepository { private DataContext context; public TractRepository(DataContext ctx) => context = ctx; pu

有没有办法将linq语句的select子句存储为变量

我有下面的类,并且select子句是重复的。我可以以某种方式将它存储为变量,这样我就不需要重复它了吗

 public class TractRepository : ITractRepository
    {
        private DataContext context;

        public TractRepository(DataContext ctx) => context = ctx;
        public async Task<IEnumerable<Tract>> GetAllAsync() => 
            await context.Tracts.Include(p => p.ContractType).Include(p => p.ContractSubType).OrderByDescending(p => p.Id)
            .Select(p => new Tract
            {
                Id = p.Id,
                Acreage = p.Acreage,
                Administrative = p.Administrative,
                ContractType = new ContractType
                {
                    Id = p.ContractType.Id,
                    ContractTypeName = p.ContractType.ContractTypeName
                },
                ContractSubType = new ContractSubType
                {
                    Id = p.ContractSubType.Id,
                    ContractSubTypeName = p.ContractSubType.ContractSubTypeName
                }
            })
            .ToListAsync();

        public async Task<Tract> GetByIdAsync(long id) =>
            await context.Tracts.Include(p => p.ContractType).Include(p => p.ContractSubType)
             .Select(p => new Tract
             {
                 Id = p.Id,
                 Acreage = p.Acreage,
                 Administrative = p.Administrative,
                 ContractType = new ContractType
                 {
                     Id = p.ContractType.Id,
                     ContractTypeName = p.ContractType.ContractTypeName
                 },
                 ContractSubType = new ContractSubType
                 {
                     Id = p.ContractSubType.Id,
                     ContractSubTypeName = p.ContractSubType.ContractSubTypeName
                 }
             }).FirstOrDefaultAsync(p => p.Id == id);

    }

您可以提取整个通用IQueryable以分离属性/方法。由于LINQ查询只有在被称为延迟执行的情况下才会执行,因此它可以用作组合其他查询的基础,例如

private IQueryable<Tract> Query() => context.Tracts
    //.Include(p => p.ContractType)
    //.Include(p => p.ContractSubType)
    .Select(p => new Tract
    {
        Id = p.Id,
        Acreage = p.Acreage,
        Administrative = p.Administrative,
        ContractType = new ContractType
        {
            Id = p.ContractType.Id,
            ContractTypeName = p.ContractType.ContractTypeName
        },
        ContractSubType = new ContractSubType
        {
            Id = p.ContractSubType.Id,
            ContractSubTypeName = p.ContractSubType.ContractSubTypeName
        }
    });


您可以提取整个通用IQueryable以分离属性/方法。由于LINQ查询只有在被称为延迟执行的情况下才会执行,因此它可以用作组合其他查询的基础,例如

private IQueryable<Tract> Query() => context.Tracts
    //.Include(p => p.ContractType)
    //.Include(p => p.ContractSubType)
    .Select(p => new Tract
    {
        Id = p.Id,
        Acreage = p.Acreage,
        Administrative = p.Administrative,
        ContractType = new ContractType
        {
            Id = p.ContractType.Id,
            ContractTypeName = p.ContractType.ContractTypeName
        },
        ContractSubType = new ContractSubType
        {
            Id = p.ContractSubType.Id,
            ContractSubTypeName = p.ContractSubType.ContractSubTypeName
        }
    });

public async Task<Tract> GetByIdAsync(long id) =>
    await Query().FirstOrDefaultAsync(p => p.Id == id);