erotik film
bodyheat full moves www xxx kajal video la figa che sborra ver video de sexo porno
Luxury replica watches
sex
asyabahis
escort antalya

** THIS SITE IS AN ARCHIVE ** - New Content is at:
BlazorHelpWebsite.com

Apr 11

Written by: Michael Washington
4/11/2013 9:43 PM  RssIcon

The current version of the LightSwitch HTML Client does not expose the computed properties created at the Entity level. Here are a few methods you can use.

image

In LightSwitch, we can create a computed column and use the following code for the result:

 

        partial void OrderTotal_Compute(ref decimal? result)
        {
            decimal dOrderTotal = 0.0M;
            // Get OrderDetails that have products
            var colOrderDetails = from OrderDetails in this.FlowerShopOrderDetail
                                  where OrderDetails.FlowerShopProduct != null
                                  select OrderDetails;
            foreach (var order in colOrderDetails)
            {
                dOrderTotal = dOrderTotal + (order.Quantity * order.FlowerShopProduct.Price);
            }
            result = dOrderTotal;
        }

 

When we consume the Entity in the LightSwitch Silverlight client…

image

The value shows.

image

However, when we create a page with the LightSwitch HTML Client we don’t have access to the computed property.

 

Using JavaScript

We can create the computed property using JavaScript.

 image

First, we Add Data Item to the Visual Studio LightSwitch HTML Client screen.

image

We add an Integer property to the screen called TotalOfOrders, and we drag it to the screen layout.

image

We click on the screen element, and in the Properties, we select Edit PostRender Code.

We use the following code:

 

myapp.AddEditFlowerShopOrder.NumberOfDetails_postRender = function (element, contentItem) {
    function updateTotal() {
        // Compute the total for the Order
        contentItem.screen.TotalOfOrders =
            TotalOrders(contentItem.screen.FlowerShopOrderDetail);
    }
    // Set a dataBind to update the value when the collection changes
    contentItem.dataBind("screen.FlowerShopOrderDetail.count", updateTotal)
};
// Function to compute the total for the Order 
function TotalOrders(OrderDetails) {
    // Start with 0
    var TotalAmountOfOrders = 0;
    // Get the data for the collection passed
    var OrderDetail = OrderDetails.data;
    // Loop through each row
    OrderDetail.forEach(function (order) {
        // Add each row to TotalAmountOfOrders
        TotalAmountOfOrders = TotalAmountOfOrders +
            (order.Quantity * order.FlowerShopProduct.Price);
    });
    // Return TotalAmountOfOrders
    return TotalAmountOfOrders;
}

 

 

image

When we run the application, the value will now show.

Note: in this example we only detect that the count of the records has changed. If for example, only the price changed, you would expect that the computed property to change, it would not. Also, if the list were a paged list and there were more records than the current page setting (the default is 45 records per page) it would not update properly.

However, in this example we return the user to the main screen after making any change, when they return to this screen the value is always properly updated.

 

Using WCF RIA Service

The limitation with the method above is that you would not be able to query or sort on the computed value when used in a collection. A WCF RIA Service resolves any limitations.

We follow the article: Shape Your LightSwitch OData Using WCF RIA Services.

Note: With the latest version of Visual Studio LightSwitch HTML Client you need to connect to "ApplicationDataObjectContext.cs", change "ApplicationDataObjectContext" (in the code) to "ApplicationData" and use: "using LightSwitchApplication.Implementation;" in the using statement section.

 

We use the following code for the method:

 

      [Query(IsDefault = true)]
        public IQueryable<EnhancedFlowerShopOrder> GetAllOrders()
        {
            // Get all the Orders
            var colFlowerShopOrders = from Order in this.Context.FlowerShopOrders
                                      // Shape the results into the 
                                      // EnhancedFlowerShopOrder class
                                      select new EnhancedFlowerShopOrder
                                      {
                                          // The Order ID
                                          ID = Order.Id,
                                          // The Order Date
                                          OrderDate = Order.OrderDate,
                                          // The first name of the Customer
                                          FirstName = Order.FlowerShopCustomer.FirstName,
                                          // The last name of the Customer
                                          LastName = Order.FlowerShopCustomer.LastName,
                                          // The order Total
                                          OrderTotal =
                                          // Get all order details lines of the Order
                                          (from FlowerShopOrderDetail in Order.FlowerShopOrderDetail
                                           // Group the products in the Order Details
                                           group FlowerShopOrderDetail 
                                           by FlowerShopOrderDetail.Id into g
                                           // Shape a new entity
                                           select new
                                           {
                                               // Create a total property that is the Quantity times the
                                               // Product price
                                               TotalOrder = g.Sum(x => x.Quantity) 
                                               * g.Sum(x => x.FlowerShopProduct.Price),
                                           }).Sum(x => x.TotalOrder) // Add the sum of all the TotalOrders
                                      };
            return colFlowerShopOrders;
        }

 

image

When we consume the Entity, the property shows up  in the screen designer like a normal field.

 

image

The computed value shows up in a list…

image

… and in a detail record.

 

 

Special Thanks

A special thanks to Stephen Provine for his assistance.

 

Download Code

The LightSwitch project is available at http://lightswitchhelpwebsite.com/Downloads.aspx

(you must have Visual Studio 2012 Update 2 installed to run the code)

18 comment(s) so far...


Hi Michael

I have migrated my project and created a RIA service. After adding "ApplicationDataObjectContext.cs" and "using LightSwitchApplication.Implementation;" still getting the below error

Error 1 The type or namespace name 'ApplicationDataObjectContext' could not be found (are you missing a using directive or an assembly reference?) F:\Samples\Visual_Studio_2012\LightSwitchHtmlRia\WcfRiaService\RiaDomainService.cs 19 17 WcfRiaService

Can you please help me out to solve this?

Your reply appreciated.

Thanks

By Rashmi on   4/11/2013 10:08 PM

@Rashmi - Download the code for this article and compare it to your own.

By Michael Washington on   4/11/2013 10:13 PM

it has only trouble using RIA Service! After get data, if you update some items, the computed fild doesn´t refresh automaticly. You need to press F5 to refresh the screen

By Roberto Filho on   5/1/2013 5:35 PM

@Roberto Filho - The WCF RIA Service is searchable and sortable. This is why I show two different methods.

By Michael Washington on   5/1/2013 5:36 PM

So...basically computed properties are pretty much useless when using the HTML client (at least for displaying data)? We may as well just compute them *on the client*?

By Stuart Dobson on   6/2/2013 5:12 AM

@Stuart Dobson - For now, but that may change in a future release.

By Michael Washington on   6/2/2013 5:13 AM

I'm using VS2013.

I'm also missing the ApplicationDataObjectContext.cs in my generatedArtifacts folder.

Does this mean that I can't use WCF RIA? I want to use it because I have to deal with data from multiple databases.

Great site and great book. I hope there is a solution.

Of course, I suppose I could copy yours and edit it since it obviously won't get overwritten but I don't particularly like that solution.

By BillRossPaxEDI on   11/22/2013 1:09 PM

@BillRossPaxEDI - If you are using Visual Studio 2013 see: http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/2226/Creating-a-WCF-RIA-Service-for-Visual-Studio-2013.aspx

By Michael Washington on   11/22/2013 2:12 PM

My assumption is that the file ApplicationDataObjectContext.cs is always that name and not application specific, yes? Because I have a "MyAppDataObjectContext.cs in that folder and it looks very similar to yours.

By BillRossPaxEDI on   11/22/2013 3:14 PM

@BillRossPaxEDI - If you have an attached data source the name may be different. I am not sure because I don't use attached data sources. My tutorial should work as described.

By Michael Washington on   11/22/2013 3:21 PM

Assuming you wanted to calculate an average, picture a situation where you have x(count)students and y(totalmarks obtained). you want to get the mean score for a particular course/unit.( TOTAL MARKS OBTAINED FOR THE PARTICULAR COURSE/UNIT) divided by (TOTAL COUNT OF STUDENTS). How would you go about this in lightswitch 2013 c#. Please

By Moses Maina on   1/9/2014 8:41 AM

@Moses Maina - You will want to make a post in the Official LightSwitch forums at: http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=lightswitch

By Michael Washington on   1/9/2014 8:42 AM

HI Micheal,

This is Parvez I just need sample for Creating Master Page with Header and menu Bar in Lightswitch using Visual Studio 2013

Can u just give me a Simple Solution

Regards

By Parvez on   11/1/2014 1:09 AM

@Parvez - I am sorry but I cannot fulfil requests.

By Michael Washington on   11/1/2014 4:53 AM

Hi Michael,

I bought one of your books Creating HTML 5 Websites using Lightswitch and I really love your book. I need your help because I am stuck in one issue, and I'm really new with Javascript. Here's my situation, I am trying to create a POS system with ligthswitch VS2013. I have a screen for SalesHeader and Sales Detail. When I add a product it automatically updates the data using binding. Everything works well, the only problem i have is the "Total Amount" it only recalculates when I click the add product button. Where it should recalculate after I add a new product.

In your notes above:

"Note: in this example we only detect that the count of the records has changed. If for example, only the price changed, you would expect that the computed property to change, it would not. Also, if the list were a paged list and there were more records than the current page setting (the default is 45 records per page) it would not update properly.

However, in this example we return the user to the main screen after making any change, when they return to this screen the value is always properly updated."

Would this be the solution? If yes, how do I refresh the screen? Because after the user select the product it actually returns to the main screen. I think I just need to refresh every time the user selects/add a product.

Any help is highly appreciated.

Thanks,
Aldwin M.

By Aldwin on   5/24/2015 3:39 PM

@Aldwin - Thank you for your support. However, please post questions for assistance to the Official LightSwitch Forums: https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=lightswitch

By Michael Washington on   5/24/2015 7:15 PM

Hi Michael, first of all thanks for your blog: i'm new to Lightswitch and your articles are very useful.

I'm approching to the html client realizing a simple project: i'm in trouble with a problem that is keeping me stuck for several days.
In simple, I've two identities:
- BOOK (id, title, id_author)
- AUTHOR (id, name, surname, birthday)

My Browse_books screen:
Tabs
-- Table (Books)
-- Table Row (rows)
-- id
-- title
-- ccName
-- ccSurname
-- Rows Layout (Author)
-- name
-- surname

ccName and ccSurname are calculated columns: in my wishes, I'll exclude Author entity from screen, showing info in these fields.
The script:
myapp.Browse_books.rows_postRender = function (element, contentItem) {
contentItem.screen.ccName =
contentItem.__value.Author.name;
};

ccName doesn't respect the author involved: in every row, for all the list, it results valorized with the author of the last item.
Can you please give my a tip?

Thanks in advance!

By 2TEDP on   6/21/2016 4:29 AM

I use WCF RIA services to selectively display fields see: http://lightswitchhelpwebsite.com/Blog/tabid/61/tagid/21/WCF-RIA-Service.aspx for more detailed help to your questions it is best to post to the official LightSwitch forums at: https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=lightswitch

By Michael Washington on   6/21/2016 4:32 AM
Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation