Error CS0029 Cannot implicitly convert type ‘System.Threading.Tasks.Task’ to ‘Type’

Error CS0029 Cannot implicitly convert type ‘System.Threading.Tasks.Task’ to ‘Type’


0

Getting the Error CS0029 Cannot implicitly convert type ‘System.Threading.Tasks.Task<peMove.Maui.Models.ErpPart>’ to ‘peMove.Maui.Models.ErpPart’.

It can’t convert the ErpPart type to an ErpPart type?? This is probably fairly straight forward but new to C# (VB coder) and async operations. I have been struggling with a lot and am just not seeing it (or burnt out).

MainPage.xmal.cs

async void OnButtonClicked(object sender, EventArgs args)
{

    DocId key = new DocId();
    key.Id1 = "Default";
    key.Id2 = "WF201B";
    key.Id3 = "000";
    ErpPart erpDocument = ErpPart.LoadAsync(key);
    }

Class ErpPart code (partial)

    public static async Task<ErpPart> LoadAsync(DocId key)
    // Static can be used with out declaring an instance.  Non-Static will require and instance.
    {
        string qlQuery = "{ products(filter:{and: [{fac: {eq: "" + key.Id1 + "" }}, {fpartno: {eq: "" + key.Id2 + ""}}, {frev: {eq: "" + key.Id3 + "" }} ]}) " +
                        @"{
                                items {
                                    id1 : fpartno
                                    id2 : frev
                                    id3 : fac
                                    id4 : fac
                                    name : fdescript
                                    OnHand {
                                        locations: items {
                                            location: flocation
                                            bin : fbinno
                                            lotsn : flot
                                            onhand : fonhand
                                        }
                                    }
                                    uofm : fmeasure
                                    source : fsource
                                    purchased : fcpurchase
                                    status : fcstscode
                                }
                            }
                        }";

    ErpPart retPart = new ErpPart();
    Item doc = new ();
    Item[] docs = null;

    try
    {
        var rootObj = new RootObject();
        //rootObj = GetDocumentAsync(qlQuery).GetAwaiter().GetResult();
        rootObj = await GetDocumentAsync(qlQuery);
        docs = rootObj.products.items;
    }
    catch (Exception ex)
    {
        Debug.Print(ex.Message);
    }

    if (docs != null)
    {
        foreach (var row in docs)
        {
            doc.Id1 = row.Id1;
            doc.Id2 = row.Id2;
            doc.Id3 = row.Id3;
            doc.Id4 = row.Id4;
            doc.Name = row.Name;
            doc.Type = "Part";
            doc.Onhand = row.Onhand;
            doc.UofM = row.UofM;
            doc.Source = row.Source;
            doc.Purchased = row.Purchased;
            doc.Status = row.Status;
            //doc.Color = MauiProgram.colorPart;

            Location[] inventory = row.Onhand.Locations;
            Debug.Print("");
            decimal onhand = 0;
            if (inventory != null && inventory.Length > 0)
            {
                // Code goes here
            }
            else
            {
                //Debug.Print("No OnHand Exists.");
            }
        }
    }
    else
    {
        Debug.Print("No matching Parts found in query.");
    }

    retPart = (ErpPart)doc;
    return (ErpPart)doc;
}

public static explicit operator ErpPart(Item v)
{
    ErpPart p = new ErpPart();
    p.Part.Id1= v.Id1;
    p.Part.Id2= v.Id2;  
    p.Part.Id3= v.Id3;
    p.Part.Id4= v.Id4;
    p.Part.Name= v.Name;
    p.Part.Type= v.Type;
    p.Part.Onhand=v.Onhand;
    p.Part.UofM=v.UofM;
    p.Part.Source=v.Source;
    p.Part.Purchased=v.Purchased;
    p.Part.Status = v.Status;
    return p;
}

private static async Task<RootObject> GetDocumentAsync(string iQuery)
{

    var graphQLOptions = new GraphQLHttpClientOptions
    {
        EndPoint = new Uri($"{Url}/graphql", UriKind.Absolute)
    };

    var graphQLHttpClient = new GraphQLHttpClient(graphQLOptions, new NewtonsoftJsonSerializer());

    var documentRequest = new GraphQLRequest { Query = iQuery };

    try
    {
        var graphQLResponse = await graphQLHttpClient.SendQueryAsync<RootObject>(documentRequest);
        Debug.Print("");
        return null;
    }
    catch (Exception ex)
    {
        Debug.Print(ex.Message);
        return null;
    }

}

2

  • 1

    You probably want await ErpPart.LoadAsync(key)

    – JonasH

    1 hour ago

  • 1

    ErpPart erpDocument = await ErpPart.LoadAsync(key); – do not forget to await the task.

    – Guru Stron

    1 hour ago


1 Answer
1


1

It can’t convert the ErpPart type to an ErpPart type??

You’re overlooking the Task part of the error:

Cannot implicitly convert type ‘System.Threading.Tasks.Task<peMove.Maui.Models.ErpPart>’ to ‘peMove.Maui.Models.ErpPart’

Which is what this method returns:

public static async Task<ErpPart> LoadAsync(DocId key)

But note how you’re trying to use the result:

ErpPart erpDocument = ErpPart.LoadAsync(key);

The LoadAsync method returns that object, but wrapped in a Task because it’s an async method. You need to await it, just like you await other async methods:

ErpPart erpDocument = await ErpPart.LoadAsync(key);



Leave a Reply

Your email address will not be published. Required fields are marked *