EF通过动态表字段进行搜索

huangapple go评论55阅读模式
英文:

EF searching by fields from dynamic table

问题

我有3个共享公共字段的表格

class T1 : IHasUserID
{
    public string Id { get; set; }
    public string UserId { get; set; }
    //其他属性
}

class T2 : IHasUserID
{
    public string Id { get; set; }
    public string UserId { get; set; }
    //其他属性
}

class T3 : IHasUserID
{
    public string Id { get; set; }
    public string UserId { get; set; }
    //其他属性
}

实体类型在运行时定义,按Id查找很容易,因为它是主键,所以我可以这样写:

var res = (IHasUserId)dbContext.FindAsync(ctx.TableType, id)

但如何按UserId搜索呢?

英文:

I have 3 tables that share common fields

class T1:IHasUserID
{
    public string Id {get;set;}
    public string UserId {get;set;}
    //other properties
}
class T2:IHasUserID
{
    public string Id {get;set;}
    public string UserId {get;set;}
    //other properties
}
class T3:IHasUserID
{
    public string Id {get;set;}
    public string UserId {get;set;}
    //other properties
}

type of entity is defined at runtime
finding by Id is easy as it's PK so I can write like this:

var res = (IHasUserId) dbContext.FindAsync(ctx.TableType, id)

but how do I search by UserId

答案1

得分: 0

不确定这是否是您想要做的,但通常我会像这样按UserID搜索:

var tableRow = ctx.TableType.Where(tt => tt.UserId == id).FirstOrDefault();

另外,我会在 IHasUserID 中添加ID和UserId。这是因为您的T1、T2和T3表是IHasUserID的扩展,应该包含在依赖于它的所有表之间共同的变量。

像这样:

class IHasUserID
{
    public string Id { get; set; }
    public string UserId { get; set; }
}

class T1 : IHasUserID
{
    //其他属性
}

class T2 : IHasUserID
{
    //其他属性
}

class T3 : IHasUserID
{
    //其他属性
}
英文:

Unsure if this is what you're trying to do, but to search by UserID i usually do it like this

var tableRow = ctx.TableType.Where(tt=> tt.UserId == id).FirstOrDefault();

Also I would add both ID and UserId in IHasUserID. This is because your T1, T2, T3 tables are extensions to IHasUserID, and should hold the variables that are common between all tables depending on it.

So like this:

class IHasUserID
{
    public string Id {get;set;}
    public string UserId {get;set;}
}
class T1:IHasUserID
{
    //other properties
}
class T2:IHasUserID
{
    //other properties
}
class T3:IHasUserID
{
    //other properties
}

答案2

得分: -1

感谢所有评论的方式,我已经创建了通用方法:

private async ValueTask<int?> GetId<TTable>(string userId) where TTable : IHasUserID
{
    TSagaTable res = await dbContext.Set<TSagaTable>().FirstAsync(_ => _.UserId == userId);

    return res.Id;
}

然后像这样运行它:

public async ValueTask<int?> GetIdByUser(string kind, string userId)
{
    Type tableType = kindProvider.GetAll().First(x => x.Kind == kind).JsonTableType;

    MethodInfo method = typeof(IdResolver).GetMethod(nameof(GetId), BindingFlags.NonPublic | BindingFlags.Instance)!;
    MethodInfo generic = method.MakeGenericMethod(tableType);

    return await (ValueTask<int?>)generic.Invoke(this, new object[] { userId });
}
英文:

thanks to all comments the way I did it I've created generic method

    private async ValueTask&lt;int?&gt; GetId&lt;TTable&gt;(string userId) where TTable: IHasUserID
    {
        TSagaTable res = await dbContext.Set&lt;TSagaTable&gt;().FirstAsync(_ =&gt; _.UserId == userId );

        return res.Id
    }

and run it like so

    public async ValueTask&lt;int?&gt; GetIdByUser(string kind, string userId)
    {
        Type tableType = kindProvider.GetAll().First(x =&gt; x.Kind== kind).JsonTableType;

        MethodInfo method = typeof(IdResolver).GetMethod(nameof(GetId),BindingFlags.NonPublic | BindingFlags.Instance)!;
        MethodInfo generic = method.MakeGenericMethod(tableType);

        return await (ValueTask&lt;int?&gt;) generic.Invoke(this,new object []{ userId});
    }

huangapple
  • 本文由 发表于 2023年6月22日 18:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定