Base solution for your next web application
Open Closed

dto linq #827


User avatar
0
iamnish created

hi

my salesdto contains one extra propert 'Amount' which is not present in sale model. Amount is the result of price*weight in child saledetails table

question is how and in which module I should fill up the amount property of saledto

Please be as elaborate as possible as I am not a seasonal programmer , my salerepository is mentioned below.

thanks :?:

public ListResultOutput<Salelistdto> GetSale(Getsaleinput input)

    {
        var sales = _saleRepository
            .GetAll()
            .Include(p => p.Saledetails.Select( c=> c.Price*c.Weight) )
             .WhereIf(
                !input.Filter.IsNullOrEmpty(),
                p => p.Invno.Contains(input.Filter) ||
                     p.Invdt.Equals(input.Filter) ||
                     p.Custid.Contains(input.Filter)
            )
            .OrderBy(p => p.Invno)
            .ThenBy(p => p.Invdt)
            .ToList();

        return new ListResultOutput&lt;Salelistdto&gt;(sales.MapTo&lt;List&lt;Salelistdto&gt;>());
    }

12 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Accourding to your code, it can be something like that:

    public ListResultOutput<Salelistdto> GetSale(Getsaleinput input)
    {
        var sales = _saleRepository
        .GetAll()
        .Include(p => p.Saledetails)
        .WhereIf(
            !input.Filter.IsNullOrEmpty(),
            p => p.Invno.Contains(input.Filter) ||
            p.Invdt.Equals(input.Filter) ||
            p.Custid.Contains(input.Filter)
        )
        .OrderBy(p => p.Invno)
        .ThenBy(p => p.Invdt)
        .ToList();
    
        var dtos = sales.Select(s => {
            var dto = s.MapTo<Salelistdto>();
            dto.Amount = s.Saledetails.Sum(c=>c.Price*c.Weight)
            return dto;
        }).ToList();
    
        return new ListResultOutput<Salelistdto>(dtos);
    }
    
  • User Avatar
    0
    iamnish created

    if I am changing the out put to json its giving following error

    Server Error in '/' Application. Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Saledetails_77F91B59002A13BD27073425BCB8F0D7F115C48F2B66059C52B6900EA3B2CA32'. Path 'result.items[0].saledetails[0].salemast.saledetails'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: Newtonsoft.Json.JsonSerializationException: Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Saledetails_77F91B59002A13BD27073425BCB8F0D7F115C48F2B66059C52B6900EA3B2CA32'. Path 'result.items[0].saledetails[0].salemast.saledetails'.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [JsonSerializationException: Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Saledetails_77F91B59002A13BD27073425BCB8F0D7F115C48F2B66059C52B6900EA3B2CA32'. Path 'result.items[0].saledetails[0].salemast.saledetails'.] Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty) +649 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) +654 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) +578 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) +492 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) +471 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) +492 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) +471 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) +654 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) +578 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) +492 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) +471 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) +654 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) +578 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) +492 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) +471 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) +492 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) +471 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) +390 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) +1929 Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType) +45 Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer) +205 Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings) +82 Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings) +47 Abp.Web.Mvc.Controllers.Results.AbpJsonResult.ExecuteResult(ControllerContext context) +531 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +106 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +62 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +70 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9742689 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

  • User Avatar
    0
    hikalkan created
    Support Team

    Your DTOs should not contain Entities. Can you share DTOs.

  • User Avatar
    0
    iamnish created

    thanks for a quick reply u r right i have the collection saledetails entity instead of saledetailsdto here is my dto

    using System; using System.Collections.ObjectModel; using Abp.Application.Services.Dto; using Abp.AutoMapper; using nn.Saleapp.Dto; using System.ComponentModel.DataAnnotations;

    namespace nn.Saleapp.Dto {

     [AutoMapFrom(typeof(Salemast))]
    public class Salelistdto : FullAuditedEntityDto
    {
    
      [Required]
        public  int TenantId { get; set; }
    
          [Required]
        public  System.DateTime Invdt { get; set; }
    
          [Required]
        public  System.DateTime Invduedt { get; set; }
    
          [Required]
        public  string Currency { get; set; }
    
          [Required]
        public  decimal Currate { get; set; }
    
          [Required]
        public  string Custid { get; set; }
        public  bool Export { get; set; }
        public  string Comments { get; set; }
    
          [Required]
        public  string Invno { get; set; }
    
       
        public  int Sslno { get; set; }
    
        public  string Branchid { get; set; }
    
        public decimal Amount { get; set; }
    
        public Collection&lt;Saledetails&gt; Saledetails { get; set; } // here i made the mistake
    }
    
  • User Avatar
    0
    iamnish created

    I have followup questions

    1 should i make separate repository and dtos for the all entities including the child entity?

    2.I dont want to send extra info(tenantid , creationdate etc ) to the the client side, but they are automatically added by the platform.

    3.why my tenantid is showing zero

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    1. You may not create repository unless you need to directly query your entities. But, ABP automatically creates IRepository<YourEntity>. So, no need to create repository at all.

    2. If you create DTO (without these fields) and map your entity to DTO, then these fields will not be sent to client. This is not related to our platform, it's a common case. If you directly send Entity to client, it will always be a problem. This is because of serialization problems of entities, which is also not unique to AspNet Zero. See why we use DTOs: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Data-Transfer-Objects#DocNeedForDTOs">http://www.aspnetboilerplate.com/Pages/ ... eedForDTOs</a>

    3. If Salemast has TenantId (which is not 0), it's automatically mapped to Salelistdto.

  • User Avatar
    0
    iamnish created

    my salesdto and json response is mentioned below .though i have not defined those properties in dto but it is automatically added to the return value.

    [AutoMapFrom(typeof(Salemast))] public class Salelistdto : FullAuditedEntityDto { [Required] public int TenantId { get; set; } [Required] public System.DateTime Invdt { get; set; } [Required] public System.DateTime Invduedt { get; set; } [Required] public string Currency { get; set; } [Required] public decimal Currate { get; set; } [Required] public string Custid { get; set; } public bool Export { get; set; } public string Comments { get; set; }

          [Required]
        public  string Invno { get; set; }
        public  int Sslno { get; set; }
        public  string Branchid { get; set; }
        public decimal Amount { get; set; }
        int termvalue;
        public  int Terms
        {
            get
            {
                return Convert.ToInt32(Invduedt.Subtract(Invdt).TotalDays);
            }
            set
            {
                termvalue = value;
            }
        }
        public Collection&lt;Saledetailslistdto&gt; Saledetails { get; set; }
    
    }
    

    json response from getsale

    { "success": true, "result": { "filter": null, "items": [ { "tenantId": 0, "invdt": "2015-02-02T00:00:00", "invduedt": "2015-03-03T00:00:00", "currency": "USD", "currate": 6.00, "custid": "MrX", "export": false, "comments": "okok", "invno": "10", "sslno": 0, "branchid": null, "amount": 2000.0000, "terms": 29, "saledetails": null, <span style="color:#FF0040">"isDeleted": false, "deleterUserId": null, "deletionTime": null, "lastModificationTime": null, "lastModifierUserId": null, "creationTime": "2016-03-03T23:33:26.2537063+08:00", "creatorUserId": null, "id": 0</span> },

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You derive your DTO from FullAuditedEntityDto. Because of that, properties you marked with red are added to json result. You should derive your Salelistdto from EntityDto in order not to send those properties to client.

    If you also dont want to send TenantId to client, you can remove it from your dto.

    If you want to send TenantId to client, can you share Salemast dto? So we can try to understand problem better.

  • User Avatar
    0
    iamnish created

    Hi thanks I changed it to Entitydto and those fields are gone but still there is one extra field 'id' .And tenant id keep displaying zero

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you also dont want to send Id field to client, dont derive from any class. You can define Salelistdto as a simple class.

    If you can share your Salemast class i can check for the TenantId being 0 problem.

    You can also check documentation about Entities & DTOs, it might be helpful as well.

    <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Entities">http://aspnetboilerplate.com/Pages/Documents/Entities</a> <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Data-Transfer-Objects">http://aspnetboilerplate.com/Pages/Docu ... er-Objects</a>

  • User Avatar
    0
    iamnish created

    by changing it to Entitydto , am i loosing anything :?: ?

  • User Avatar
    0
    hikalkan created
    Support Team

    No, just Id goes away as you wanted it.