小编Gio*_*iox的帖子

LINQ 组求和查询中的 Bool 到 int

我继承了一个应用程序,需要添加一个新功能,但现有数据库和应用程序的设计都很糟糕。

我需要使用 LINQ 查询提取所有员工及其文档数量以及已打开的文档数量。

为了对文档进行计数,我做了一个简单的操作count(),对于打开的数字,我有一个布尔字段,指示文档是否已打开。如果这个 lat 是一个值为 0 或 1 的整数,那就非常简单了,只需对该字段求和即可。

我也尝试使用布尔值来完成此操作,但失败了,因为我无法在 Linq-to-Entities 中使用 Convert.toInt32 :


var employees = from e in context.Employees
                join d in context.EmployeeDocuments on e.EmployeeId equals d.EmployeeId into dj
                from d in dj.DefaultIfEmpty()
                group new { e, d } by new { e.EmployeeId, e.FirstName, e.LastName, e.FiscalCode, e.IdentificationNumber, e.EmploymentStartDate } into g
                select new EmployeeListItem
                           {
                               EmployeeId = g.Key.EmployeeId,
                               FirstName = g.Key.FirstName,
                               LastName = g.Key.LastName,
                               FiscalCode = g.Key.FiscalCode,
                               IdentificationNumber = g.Key.IdentificationNumber,
                               EmploymentStartDate = g.Key.EmploymentStartDate.ToString("dd/MM/yyyy"),
                               DocumentCount …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-entities entity-framework-core

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

EEST的ElasticSearch索引/插入失败

我正在尝试将一些JSON数据插入到弹性搜索中进行测试.

这是代码:

    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(node);        
    settings.DefaultIndex("FormId");

    var client = new ElasticClient(settings);

    var myJson = @"{ ""hello"" : ""world"" }";
    var response = client.Index(myJson, i => i.Index("FormId")
                        .Type("resp")
                        .Id((int)r.id)
                        .Refresh()
                        );
Run Code Online (Sandbox Code Playgroud)

没有插入任何内容,我从ES收到以下错误:{在PUT上对无效的低级别调用构建的无效NEST响应:/ FormId/resp/1?refresh = true}

我试图找到一些例子,但都使用预定义的数据结构,而我想使用非结构化数据的JSON数据.

以上错误消息来自NEST.弹性回复(并在日志中写入)以下消息:MapperParsingException [无法解析]; nested- NotXContentException [压缩器检测只能在某些xcontent字节或压缩的xcontent字节上调用];

无法解析{""你好"":""世界""} ????

c# json elasticsearch nest kibana

3
推荐指数
1
解决办法
8476
查看次数

获取控制器内请求的 WebApi 版本

我在 Asp.NET Core 3 WebApi 项目中添加了 WebAPI 的版本控制:


public void ConfigureServices(IServiceCollection services)
{
    services.AddApiVersioning(
                        options =>
                        {
                            // reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
                            options.ReportApiVersions = true;
                        });
    services.AddVersionedApiExplorer(
        options =>
        {
            options.GroupNameFormat = "'v'VVV";
            options.SubstituteApiVersionInUrl = true;
        });

    ...other configs...
}
Run Code Online (Sandbox Code Playgroud)

所以现在在我的控制器中我可以添加版本:


[Route("[controller]")]
[ApiController]
public class SurveysController : ControllerBase
{
    [HttpGet]
    [ApiVersion("1.0")]
    public ActionResult Get(int adm0Code = 0, int page = 1)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,在 API 响应中,有时我还包含指向其他端点的链接。例如:

OriginalCsvFile = (s.SurveyStatusID==2) ? (baseUrl + "/Surveys/ExportToCSV?surveyID="+ …
Run Code Online (Sandbox Code Playgroud)

c# versioning asp.net-web-api asp.net-core

3
推荐指数
1
解决办法
4860
查看次数

asp.net core webapi frombody参数大小限制

我在 ASP.NET Core 3.1 中实现了一个端点来检索大型 JSON 对象,当此 JSON 开始相对较大时,我遇到了问题。我开始在 5~600KB 左右出现问题。

对于正文低于 600~500KB 的请求,一切正常。

端点定义如下:

[DisableRequestSizeLimit]
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
[HttpPost]
[Route("MyTestEndpoint")]
public void PostTest([FromBody]object objVal)
{
    // objVal is null when the post is larger than ~5~600KB
    string body = objVal.toString;
    ....
}
Run Code Online (Sandbox Code Playgroud)

网络配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
<system.web>
    <httpRuntime  maxRequestLength="1048576" />  
</system.web>

    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\ApiSLPCalendar.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
      <security>
        <requestFiltering>
          <!-- This will handle requests …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api asp.net-core

3
推荐指数
1
解决办法
3809
查看次数

实体框架 6 找不到 &lt;model&gt;.&lt;entity&gt; 的 CLR 类型

我有一个 web 应用程序,asp.net web-form 4.5 C# with EF6 DB-first,我从将近一年的时间开始研究它,直到今天,当我尝试向edmx 从数据库更新。添加表时没有错误,但从代码中看不到,如果我尝试附加到 EntityDataSource,我会收到消息“找不到 CLR 类型。

我已经看到代码生成策略设置为T4。如果我更改为“ Legacy ObjectContext ”,新实体在代码中正确显示并且 EntityDataSource 工作正常,我可以完美地看到关联的 gridview,但是如果我尝试导航到其他一些页面,我可以访问其他“旧”实体,我收到几行错误:

指定的架构无效。错误: CLR 类型到 EDM 类型的映射不明确,因为多个 CLR 类型与 EDM 类型“报告”相匹配。之前发现的 CLR 类型 'Report' ,新发现的 CLR 类型 'AppNameModel.Report'。CLR 类型到 EDM 类型的映射是不明确的,因为多个 CLR 类型与 EDM 类型“客户”相匹配。以前找到的 CLR 类型 'Customer',新找到的 CLR 类型 'AppNameModel.Customer'。.... 等等所有以前创建的实体(最后一个未列出并且适用于遗留的 objCtx 策略!)

那么EF有什么问题??!我已经阅读了有关堆栈溢出的所有问答,但似乎都没有解决方案。

我将把 web.config 的一部分:

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
Run Code Online (Sandbox Code Playgroud)

...

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters> …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net entity entity-framework

2
推荐指数
1
解决办法
5878
查看次数

LINQ 从多个联接中获取具有最大日期的记录

我有三个表:课程、课程位置、课程时间表

每门课程可以在一个或多个地点举行(一对多) 每个地点可以举办一个或多个时间表(一对多)

我需要获取具有 Schedules.Date> Today 的所有课程(唯一名称),并显示表 CourseSchedules 中包含的日期的最大值

我当前的 linq 代码是:

var courses = (from c in db.Courses
           join cl in db.CourseLocations on c.CourseID equals cl.CourseID
           join cs in db.CourseSchedules on cl.CourseLocationID equals cs.CourseLocationID 

           where c.CourseStatusID == 1 && c.DeleteDate == null && ((c.CourseCategoryID == 1 && cs.EndDate >= courseEndDateFilter) || (c.CourseCategoryID == 3))

           select new
           {
               c.CourseID,
               CourseName = c.Name,
               CourseEndDate = cs.EndDate
           }).Distinct().OrderBy(o => o.CourseCategoryID).ThenBy(o => o.CourseName);
Run Code Online (Sandbox Code Playgroud)

其中 courseEndDateFilter 是用于定义要过滤的日期的变量。

上述查询的问题是我得到了所有重复的课程,而不仅仅是具有 MAX 值 cs.EndDate 的课程

有没有办法(有效)做到这一点?

.net c# sql linq

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

NET Core 3.1 应用程序设置中的连接字符串与 EntityFramework Core

我有一个 WEB API 应用程序,模板(或 EF Core)已在上下文所在的文件中设置了连接字符串。我想从那里删除它并将其放入 appsettings 文件中并在 DbContext 的 OnConfiguring() 方法中读取它。

public partial class ReportingContext : DbContext
{
    public MyAppContext() {}

    public MyAppContext(DbContextOptions<MyAppContext> options) : base(options)  {  }


    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("my-connection-string", x => x.UseNetTopologySuite());
            //I want to remove the connection string from the above line, by reading from the appsettings file
        }
    }

}

Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它位于上下文的 OnConfiguring 方法中。例如,如果我尝试放在外部,例如在启动时,应用程序将不再工作,因为它需要 OnConfiguring 中指定的连接字符串,因为模型中的逻辑按以下方式实例化上下文:

public partial class SomeClassOfMine
{
    public static List<BusinessLogic.Dto.NearbyMarkets> GetNearbyMarkets(int adm0code, double lat, double …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api entity-framework-core asp.net-core

0
推荐指数
1
解决办法
3227
查看次数