简体   繁体   English

不能隐式转换类型 &#39;System.Collections.Generic.List<DTO.ProductivityDTO> &#39; 到 &#39;DTO.ProductivityDTO&#39;

[英]Cannot implicitly convert type 'System.Collections.Generic.List<DTO.ProductivityDTO>' to 'DTO.ProductivityDTO'

So I'm trying to retrieve data from a database to display it in a table.所以我试图从数据库中检索数据以将其显示在表中。 (This is a WebApi project). (这是一个 WebApi 项目)。 The idea is to go through all the prodactivity from the table whose status is 1. Put them in some list and return it.这个想法是通过状态为 1 的表中的所有 prodactivity。将它们放在某个列表中并返回它。 To do this, I need to convert a TBL object to DAL.为此,我需要将 TBL 对象转换为 DAL。 but here I am stuck.但在这里我被卡住了。 It is unable to perform a conversion and is therefore unwilling to return an object of a different type.它无法执行转换,因此不愿意返回不同类型的对象。 I tried to convert in all sorts of ways but it'S out of sync with the type in the controller.我试图以各种方式进行转换,但它与控制器中的类型不同步。

This is my code: (do'nt look at the other functions. Look at the function getProductivityRequest at the end)这是我的代码:(不要看其他函数。看最后的函数getProductivityRequest

UserController.cs用户控制器.cs

using BL;
using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebApiSystem.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]//SupportsCredentials = true    
    [RoutePrefix("api/User")]

    public class UserController : ApiController
    {
        [HttpGet]
        [Route("login/{email}/{pass}")]

        public IHttpActionResult LogIn(string email, string pass)
        {
            UserDTO user = BL.UserService.LogIn(email, pass);
            if (user != null)
                return Ok(user);
            return NotFound();
        }

        [Route("register")]
        public IHttpActionResult Register(UserDTO user)
        {    
            return NotFound();
        }

        [HttpPost]
        [Route("productivity")]

        public IHttpActionResult InsertProductivity([FromBody] ProductivityDTO p)
        {
            bool b = BL.UserService.InsertProductivity(p);
            if (b)
                return Ok(b);
            return BadRequest();
        }

        [HttpGet]
        [Route("getProductivityRequest")]

        public IHttpActionResult GetProductivityRequest()
        {
            return Json(BL.UserService.GetProductivityRequest());
        }    
    }
}

UserService.cs用户服务.cs

using DAL;
using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BL
{
    public class UserService
    {
        public static UserDTO LogIn(string email, string pass)
        {
            using (Model2 db = new Model2())
            {
                UserTbl user = db.UserTbl.FirstOrDefault(u => u.UserEmail == email && u.UserPassLogin == pass);
                if (user == null)
                    return null;
                return CONVERTERS.UserConverter.ConvertUsertoDTO(user);
            }
        }
        public static bool InsertProductivity(ProductivityDTO p)
        {
            using (Model2 db = new Model2())
            {
                //conver dal to dto
                ProductivityTbl prod = BL.CONVERTERS.ProductivityConverter.ProductivityDal(p);
                prod.ProductivityStatus = 1;
                db.ProductivityTbl.Add(prod);
                db.SaveChanges();
                return true;
            }
            return false;
        }

        public static ProductivityDTO GetProductivityRequest()//Here I can not convert
        {
            using (Model2 db = new Model2())
            {
                List<ProductivityDTO> PList = new List<ProductivityDTO>();

                foreach (var item in db.ProductivityTbl.ToList())
                {
                    if(item.ProductivityStatus==1)
                    {
                        ProductivityDTO prod = BL.CONVERTERS.ProductivityConverter.ConvertProductivitytoDTO(item);                    
                        PList.Add(prod);
                    }                   
                }
                return (PList);//He's unable to return the object

            }
        }
    }
}

ProductivityConverter.cs (if it's relevant...) ProductivityConverter.cs (如果相关的话...)

using DTO;
using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BL.CONVERTERS
{
    class ProductivityConverter
    {
   public static ProductivityDTO ConvertProductivitytoDTO(ProductivityTbl productivity) {
            return new ProductivityDTO
            {
                ProductivyCode = productivity.ProductivyCode,
                ProductivityNum = productivity.ProductivityNum,
                ProductivityStatus = productivity.ProductivityStatus,
                UserCode = productivity.UserCode,
                Cmment = productivity.Cmment,
                Date = productivity.Date

            };

        }

        public static ProductivityTbl ProductivityDal(ProductivityDTO productivity)
        {
            return new ProductivityTbl
            {
                ProductivyCode = productivity.ProductivyCode,
                ProductivityNum = productivity.ProductivityNum,
                ProductivityStatus = productivity.ProductivityStatus,
                UserCode = productivity.UserCode,
                Cmment = productivity.Cmment,
                Date = productivity.Date

            };
  
        }
    }
}

If anyone knows of another way to transfer the data I would love ideas.如果有人知道传输数据的另一种方式,我会喜欢的想法。

Thanks!谢谢!

Your method returns a ProductivityDTO :您的方法返回ProductivityDTO

public static ProductivityDTO GetProductivityRequest()

...but you are trying to return PList which is a List<ProductivityDTO> . ...但您正在尝试返回PList ,它是一个List<ProductivityDTO>

Your method signature should be:您的方法签名应该是:

public static List<ProductivityDTO> GetProductivityRequest()

Although your comment suggests you can't change the method signature.尽管您的评论表明您无法更改方法签名。 In which case you need to return just one DTO.在这种情况下,您只需返回一个 DTO。 How you determine which one is up to you.您如何确定哪一个取决于您。 But if it were just one with a ProductivityStatus of 1 then you could do:但如果它只是ProductivityStatus为 1 的一个,那么你可以这样做:

        public static ProductivityDTO GetProductivityRequest()//Here I can not convert
        {
            using (Model2 db = new Model2())
            {
                return BL.CONVERTERS.ProductivityConverter.ConvertProductivitytoDTO(db.ProductivityTbl.FirstOrDefault(p => p.ProductivityStatus == 1)));

            }
        }

Your function is defined to return a single ProductivityDTO .您的函数被定义为返回单个ProductivityDTO
But your function is building a List<ProductivityDTO> and returning that.但是您的函数正在构建一个List<ProductivityDTO>并返回它。

Change your function return type to a collection type, such as:将您的函数返回类型更改为集合类型,例如:

public static List<ProductivityDTO> GetProductivityRequest()

or或者

public static IEnumerable<ProductivityDTO> GetProductivityRequest()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;System.Collections.Generic.List&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'System.Collections.Generic.List' to 'string' 无法隐式转换类型System.Collections.Generic.List <char> - cannot implicitly convert type System.Collections.Generic.List<char> 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T> 如何修复“无法将类型System.Collections.Generic.List &lt;&gt;隐式转换为System.Collections.Generic.List &lt;&gt;” - How to fix 'Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>' 无法隐式转换类型'System.Collections.Generic.List<x> ' 到 'System.Collections.Generic.List<y> '</y></x> - Cannot implicitly convert type 'System.Collections.Generic.List<x>' to 'System.Collections.Generic.List<y>' 无法隐式转换类型&#39;System.Collections.Generic.List错误 - Cannot implicitly convert type 'System.Collections.Generic.List error 无法隐式转换类型&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List 无法将类型&#39;System.Collections.Generic.List &lt;&gt;&#39;隐式转换为&#39;&#39; - Cannot implicitly convert type 'System.Collections.Generic.List<>' to ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM