Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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# LINQWebAPI上的错误序列化_C#_Asp.net Mvc_Linq_Asp.net Web Api_Json.net - Fatal编程技术网

C# LINQWebAPI上的错误序列化

C# LINQWebAPI上的错误序列化,c#,asp.net-mvc,linq,asp.net-web-api,json.net,C#,Asp.net Mvc,Linq,Asp.net Web Api,Json.net,我有一个LINQ查询: public List<tblStudent> GetNames() { var result = (from student in db.tblStudents.ToList() select new tblStudent { StudentID = student.StudentID, S

我有一个LINQ查询:

public List<tblStudent> GetNames()
{
    var result = (from student in db.tblStudents.ToList()
                  select new tblStudent 
                  {
                      StudentID = student.StudentID,
                      StudentName = student.StudentName,
                      Email = student.Email,
                      IsDeleted= student.IsDeleted

                      //tblDepartment = department.tblStudents
                  });
    return result.ToList();
}
public List GetNames()
{
var result=(来自db.tblStudents.ToList()中的学生)
选择新的TBL学生
{
StudentID=student.StudentID,
StudentName=student.StudentName,
Email=student.Email,
IsDeleted=student.IsDeleted
//TBL部门=部门。TBL学生
});
返回result.ToList();
}
当我执行该查询时,它给了我一个结果:


但我不想在结果中看到空值。如何解决该问题?

ApiController返回集合时,可以使用匿名类型仅返回所需的成员

比如说

public class MyApiController : ApiController {

    //...

    public IHttpActionResult MyControllerAction() {
        var result = myService.GetNames()
                    .Select(student => select new { //<-- Note what was done here
                          StudentID = student.StudentID,
                          StudentName = student.StudentName,
                          Email = student.Email,
                          IsDeleted= student.IsDeleted
                      });
        return Ok(result.ToList());
    }
}
公共类MyApicController:ApicController{
//...
公共IHttpActionResult MyControlleration(){
var result=myService.GetNames()

.Select(student=>selectnew{/虽然我还没有尝试过,但我希望您能够将Json.NET配置为在序列化时忽略空值。在
ConfigureServices
中,编写以下内容:

services.AddJsonFormatters(设置=>settings.NullValueHandling=NullValueHandling.Ignore);

使用匿名类型也可以,但如果您有一个列表,其中某些对象的某些属性具有非空值,而其他对象的某些属性具有空值,那么这就变得更加棘手了。了解这两种方法很好,因此您可以在任何情况下使用更合适的方法。

我刚刚在我的
网站上添加了这一行ApiConfig.cs
和我的问题已经解决

 config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;

它抛出了一个错误:当前数据库中不存在名称“Ok”context@HalilŞahin我假设您是在
ApiController
中进行调用的,正如您提到的基于问题标签的Web API一样。
GetNames()是什么类型的
归属?这是一个ApiController@HalilŞahin那么该方法应该存在@HalilŞahin,只是为了确保使用正确的版本,是asp.net-core中属性
[ApiController]
中的
ApiController
,还是像asp.net-web-api-2中的
MyController:ApiController
一样用作基类+