小编red*_*510的帖子

如何在EF Core 2中为.ThenInclude编写Repository方法

我正在尝试为Entity Framework Core 2.0编写一个存储库方法,该方法可以使用.ThenInclude处理返回属性的子集合,但是我遇到了第二个表达式的问题.这是.Include的一种工作方法,它将返回实体的子属性(提供lambdas列表).

public T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties)
{
    IQueryable<T> query = _context.Set<T>();
    foreach (var includeProperty in includeProperties)
    {
        query = query.Include(includeProperty);
    } 

    return query.Where(predicate).FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)

现在,我尝试编写一个方法,该方法将采用两个表达式的元组并将它们提供给.Include(a => a.someChild).ThenInclude(b => b.aChildOfSomeChild)链.这不是一个完美的解决方案,因为它只处理一个孩子的一个孩子,但这是一个开始.

public T GetSingle(Expression<Func<T, bool>> predicate, params Tuple<Expression<Func<T, object>>, Expression<Func<T, object>>>[] includeProperties)
{
    IQueryable<T> query = _context.Set<T>();
    foreach (var includeProperty in includeProperties)
    {
         query = query.Include(includeProperty.Item1).ThenInclude(includeProperty.Item2);              
    }

    return query.Where(predicate).FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)

Intellisense返回一个错误,说"无法根据用法推断出类型,请尝试明确指定类型".我有一种感觉,因为Item2中的表达式需要被分类为与Item1有某种关联,因为它需要知道它具有的子关系.

编写这样的方法有什么想法或更好的技巧吗?

c# lambda repository-pattern entity-framework-core .net-core

14
推荐指数
3
解决办法
7060
查看次数

你怎么逃脱jsonpath中的@符号?

给出一个像这样的json列表:

    {    
 "listRel:customFieldList": {
            "platformCore:customField": [
              {
                "@internalId": "801",
                "scriptId": "custentity_admin_contact_cweb",
                "@xsi:type": "platformCore:BooleanCustomFieldRef",
                "platformCore:value": "false"
              },
              {
                "@internalId": "712",
                "@scriptId": "custentity_bar_number",
                "@xsi:type": "platformCore:StringCustomFieldRef",
                "platformCore:value": "166493"
              },
              {
                "@internalId": "798",
                "@scriptId": "custentity_contact_type",
                "@xsi:type": "platformCore:SelectCustomFieldRef",
                "platformCore:value": {
                  "@internalId": "1",
                  "@typeId": "148",
                  "platformCore:name": "Attorney"
                }
              }
              ]
 }
}
Run Code Online (Sandbox Code Playgroud)

如何在"custentity_bar_number"中选择值?166493?

这将帮助您,但前提是您在JSON中删除@scriptId前面的@符号.

$..['platformCore:customField'][?(@['scriptId'] == 'custentity_bar_number')]
Run Code Online (Sandbox Code Playgroud)

所以我需要的是一种逃避json中@符号的方法,并做出类似这样的工作:

$..['platformCore:customField'][?(@['@scriptId'] == 'custentity_bar_number')]
Run Code Online (Sandbox Code Playgroud)

我正在使用http://jsonpath.com/来尝试完成这项工作.

json jsonpath

8
推荐指数
1
解决办法
1153
查看次数

初始配置后将 Enrich.WithProperty 添加到 serilog 全局记录器

我们有一个带有 Main() 方法的服务,它像这样初始化我们的 serilog 记录器:

            Log.Logger = new LoggerConfiguration()
               .MinimumLevel.Debug()
               .WriteTo.ColoredConsole()
               .WriteTo.Seq(ConfigurationManager.AppSettings["SeqServer"], apiKey: ConfigurationManager.AppSettings["SeqApiKey"])
               .Enrich.WithProperty("ServiceName", "SomeConsumer")                   
               .CreateLogger(); 
Run Code Online (Sandbox Code Playgroud)

现在,以不同的方法,我想为这个记录器附加一个额外的属性。我想添加一个 .Enrich.WithProperty,因为我喜欢 Log.Error("Somelog")

我怎样才能做到这一点?我只想用一些字符串附加一个新属性。

.net logging serilog

4
推荐指数
1
解决办法
4030
查看次数