Question

Background:

  1. I have created a function import which is available in my context object as GetJournalViewItemsQuery()

  2. The function import returns a complex type called JournalViewItem.

  3. Now when I try to load the JournalViewItem into my application DTO called JournalEntry I come up with error:

    Error 7 Cannot implicitly convert type 'MyApp.Infrastructure.Models.JournalEntry' to 'MyApp.SqlData.JournalViewItem'

This is the code:

             var journalEntry =  Context.GetJournalViewItemsQuery()
                .Where(i => i.JournalItemId == _journalEntryId)
                .Select(x => new JournalEntry(x.JournalItemId,
                    x.HeaderText,x.JournalText, x.LastUpdatedOn,
                    x.JournalItemTypeId)).Single();

The error happens at the "new JournalEntry" line.

My question: How can I cast the JournalViewItem complex type to my DTO ?

Thank you

After @JanR suggestion I still have the same problem. The modified code is:

       var journalEntry = Context.GetJournalViewItemsQuery()
             .Where(i => i.JournalItemId == _journalEntryId)
             .Select(x => new JournalEntry
             {
                 JournalEntryNumber = x.JournalItemId,
                 HeaderText = x.HeaderText,
                 BodyText = x.JournalText,
                 LastUpdatedOn = x.LastUpdatedOn,
                 JournalEntryType = x.JournalItemTypeId
             }).Single();

I found out the reason for my problem. I failed to mention (my apologies) that I'm working off generated code from WCF RIA Domain Services for Silverlight application. As such the Context.GetJournalViewItemsQuery() needs to be executed and THEN I can query the results on my callback method using the LINQ expression that @Chuck.Net and JanR have suggested.

Here's the working code to those who might be interested:

        public IList<JournalEntryHeader> GetJournalEntryHeaders()
    {
        PerformQuery<JournalViewItem>(Context.GetJournalViewItemsQuery(), GetJournalEntryHeadersFromDbComplete);

        return _journalHeaders;
    }

    void PerformJournalEntryHeadersQuery(EntityQuery<JournalViewItem> qry,
                            EventHandler<EntityResultsArgs<JournalViewItem>> evt)
    {
        Context.Load<JournalViewItem>(qry, r =>
        {
            if (evt != null)
            {
                try
                {
                    if (r.HasError)
                    {
                        evt(this, new EntityResultsArgs<JournalViewItem>(r.Error));
                    }
                    else if (r.Entities.Count() > 0)
                    {
                        evt(this, new EntityResultsArgs<JournalViewItem>(Context.JournalViewItems));
                    }
                    else if (r.Entities.Count() == 0 && _currentJournalItemsPage > 0)
                    {
                        GetPrevPageJournalEntryHeadersAsync();
                    }
                }
                catch (Exception ex)
                {
                    evt(this, new EntityResultsArgs<JournalViewItem>(ex));
                }
            }
        }, null);
    }


    void GetJournalEntryHeadersFromDbComplete(object sender, EntityResultsArgs<JournalViewItem> e)
    {
        if (e.Error != null)
        {
            string errMsg = e.Error.Message;
        }
        else
        {
            _journalHeaders = e.Results
                .Select(
            x => new JournalEntryHeader(x.JournalItemId,
                                     x.ProjectName,
                                     x.TopicName,
                                     x.HeaderText,
                                     x.EntryTypeName,
                                     x.LastUpdatedOn)).ToList();

            GetJournalEntryHeadersComplete(this, new JournalEntryHeaderItemsEventArgs(_journalHeaders));
        }
    }
Était-ce utile?

La solution 4

I found out the reason for my problem. I failed to mention (my apologies) that I'm working off generated code from WCF RIA Domain Services for Silverlight application. As such the Context.GetJournalViewItemsQuery() needs to be executed and THEN I can query the results on my callback method using the LINQ expression that @Chuck.Net and JanR have suggested.

You'll find the answer in the original post where I entered the question.

Autres conseils

What you need to do is the following, in the new JournalEntry() function you will need to set all the properties to the JournalViewItem object.

 var journalEntry =  Context.GetJournalViewItemsQuery()
                .Where(i => i.JournalItemId == _journalEntryId)
                .Select(x => new JournalEntry {
                    JournalEntryId = x.JournalItemId,
                    HeaderText = x.HeaderText,
                    JournalText = x.JournalText
                    //etc
                    }).Single();

I am just guessing the actual property names here as I am not familiar with what the JounralEntry object looks like.

EDIT: added {}

I created a ConsoleApp to test @JanR answer. It seems to be working correctly.

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

namespace StackOverFlowConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<JournalViewItem> JournalViewItems = new List<JournalViewItem>()
            {
                new JournalViewItem(){JournalItemId =1, HeaderText="HeaderText", JournalText="JournalText", LastUpdatedOn= DateTime.Today, JournalItemTypeId=1},

            };

            int _journalEntryId = 1;

            var journalEntry = JournalViewItems
              .Where(i => i.JournalItemId == _journalEntryId)
              .Select(x => new JournalEntry
              {
                  JournalEntryNumber = x.JournalItemId,
                  HeaderText = x.HeaderText,
                  BodyText = x.JournalText,
                  LastUpdatedOn = x.LastUpdatedOn,
                  JournalEntryType = x.JournalItemTypeId
              }).Single();

        }

        class JournalViewItem
        {

            public int JournalItemId { get; set; }
            public string HeaderText { get; set; }
            public string JournalText { get; set; }
            public DateTime LastUpdatedOn { get; set; }
            public int JournalItemTypeId { get; set; }
        }

        class JournalEntry
        {

            public int JournalEntryNumber { get; set; }
            public string HeaderText { get; set; }
            public string BodyText { get; set; }
            public DateTime LastUpdatedOn { get; set; }
            public int JournalEntryType { get; set; }
        }

    }
}

I looked into complx type a little bit. I tried the following code in my own project and was able to reproduce the error you mentioned:

var result = (from y in CS.PSMBIPMTTXLOGs select new PSMBIPMTCONTROL(){MBI_PMT_TX_ID = y.MBI_PMT_TX_ID}).ToList();

But when I changed it to return anonymous type, it worked: var result = (from y in CS.PSMBIPMTTXLOGs select new {MBI_PMT_TX_ID = y.MBI_PMT_TX_ID}).ToList();

You mentioned that you have even tried creating an anonymous type instead of newing it into my DTO, but it still complains. Can you post your code for returning an anonymous type and the error message it gives? Thank you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top