Pergunta

I have the following problem - I would like to compute a field in my database based on a simple query and division. First a bit of info about my database structure:

|WorkingUnits|-1---------n-|Materials| e.i One-to-Many relation between WU and Materials.

This is the two tables and the relation between them. In the WorkingUnits table, I have a computed field called WUAmount. Also each Working Unit has a 'Price'. Same goes for the Materials - each of them has a 'Price'. In the end what I would like to achive is to sum up all the prices of all Materials assigned to a particular Working Unit and divide that by the price of the Working Unit itself.

What I am trying to do so far is the following:

    partial void WUAmount_Compute(ref decimal result)
    {
        //decimal amount = 0;
        IDataServiceQueryable<WorkingUnit> query;
        query = from WorkingUnit in this.DataWorkspace.ApplicationData.WorkingUnits
                where WorkingUnit.Materials != null
                select WorkingUnit;

        foreach (WorkingUnit detail in query)
        {
            result = this.WUPrice / (result + detail.Materials.Sum(s => s.Price).Value);
        }
}

But I get an Inner Exception:

Cannot compare elements of type 'System.Data.Objects.DataClasses.EntityCollection`1'.
Only primitive types, enumeration types and entity types are supported.

I am fairly new to Lightswitch and LINQ and cannot figure this yet. Any help is appreciated.

Foi útil?

Solução

If I understand you correctly, all you should need is:

partial void WUAmount_Compute(ref decimal result)
{
   result = (this.Materials.Sum(x => x.Price) / this.WUPrice);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top