var items = Query<Book>(new string[] { "Id", "PubTime", "Title" });
foreach (object[] row in items)
{
long id = (long)row[0];
DateTime pubTime = (DateTime)row[1];
string title = (string)row[2];
Console.WriteLine(id + "," + pubTime + "," + title);
}
IEnumerable<object[]> Query<TEntity>(string[] propNames) where TEntity : class
{
ParameterExpression exParameter = Expression.Parameter(typeof(TEntity));
List<Expression> exProps = new List<Expression>();
foreach (string propName in propNames)
{
Expression exProp = Expression.Convert(Expression.MakeMemberAccess(
exParameter, typeof(TEntity).GetProperty(propName)), typeof(object));
exProps.Add(exProp);
}
Expression[] initializers = exProps.ToArray();
NewArrayExpression newArrayExp = Expression.NewArrayInit(typeof(object), initializers);
var selectExpression = Expression.Lambda<Func<TEntity, object[]>>(newArrayExp, exParameter);
using MyDbcontext ctx = new MyDbcontext();
IQueryable<object[]> selectQueryable = ctx.Set<TEntity>().Select(selectExpression);
return selectQueryable.ToArray();
}
using ExpressionTreeDemon;
using System.Linq.Expressions;
using ExpressionTreeToString;
using Microsoft.AspNetCore.Builder;
var builder = WebApplication.CreateBuilder(args);
builder.Build().Run();
using MyDbcontext ctx = new MyDbcontext();
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NModbus4.NetCore" Version="2.0.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1" />
<PackageReference Include="ExpressionTreeToString" Version="3.4.65" />
</ItemGroup>
<ItemGroup>
<None Update="MyNode.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExpressionTreeDemon
{
public class MyDbcontext:DbContext
{
private ConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
public DbSet<Book> Books { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
cfgBuilder.AddJsonFile("MyNode.json", optional: true, reloadOnChange: true);
IConfigurationRoot cfgRoot = cfgBuilder.Build();
string conn = cfgRoot.GetSection("ConnectionStrings:MySQLConnectionString").Value;
optionsBuilder.UseMySql(conn, new MySqlServerVersion(new Version(5, 7, 35)));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
}
}
}
public class Book
{
public long Id { get; set; }
public string? Title { get; set; }
public DateTime PubTime { get; set; }
public double Price { get; set; }
public string? AuthorName { get; set; }
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
class BookEntityConfig : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
builder.ToTable("Book");
builder.HasKey(x => x.Id);
builder.Property(e => e.Title).HasMaxLength(50).IsRequired();
builder.Property(e => e.AuthorName).HasMaxLength(20).IsRequired();
}
}
{
"ConnectionStrings": {
"SqliteConnectionString": "Data Source=D:\\cSharpe\\code\\ExpressionTreeDemon\\ExpressionTreeDemon\\bin\\Debug\\net6.0\\Database\\DbSqlite.db",
"MySQLConnectionString": "server=192.168.207.107; database=MyBooks2; uid=root; pwd=123456;"
}
}