Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Forms Portable:如何基于数据库创建饼图切片?_C#_Linq_Xaml_Xamarin_Xamarin.forms - Fatal编程技术网

C# Forms Portable:如何基于数据库创建饼图切片?

C# Forms Portable:如何基于数据库创建饼图切片?,c#,linq,xaml,xamarin,xamarin.forms,C#,Linq,Xaml,Xamarin,Xamarin.forms,大家好。我正在创建一个Xamarin.Forms便携式应用程序,在这个应用程序中,我可以使用OxyPlot在那里显示一个饼图 饼图包含6个切片,所有切片都是预定义的。我的问题是,我将如何制作一个饼图,其中饼图切片基于数据库中的数据 在我的WebFormsProject中,我创建了一个SalesController,在那里我使用LINQ表达式获取数据库中我需要的所有数据。我还在那里创建了一个SalesViewModel,在那里我声明了我拥有的所有属性。我尝试在我的WEB API中测试它是否返回值,

大家好。我正在创建一个Xamarin.Forms便携式应用程序,在这个应用程序中,我可以使用OxyPlot在那里显示一个饼图

饼图包含6个切片,所有切片都是预定义的。我的问题是,我将如何制作一个饼图,其中饼图切片基于数据库中的数据

在我的WebFormsProject中,我创建了一个SalesController,在那里我使用LINQ表达式获取数据库中我需要的所有数据。我还在那里创建了一个SalesViewModel,在那里我声明了我拥有的所有属性。我尝试在我的WEB API中测试它是否返回值,它是否返回值

在我的便携项目中,我创建了一个Sales模型,该模型的属性与WebFormsProject中的SalesViewModel完全相同。我还有一个SalesVM.cs,其中我尝试使用SalesServices和RestClient从WebFormsProject访问记录

这些是我用过的代码

WebForms

1。)SalesViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebFormsDemo.ViewModel
{
    public class SalesViewModel
    {
        public int Id { get; set; }
        public int ORDER_ID { get; set; }
        public int ORDER_DETAILS_ID { get; set; }
        public int PRODUCT_ID { get; set; }
        public string PRODUCT_CODE { get; set; }
        public string NET_AMOUNT { get; set; }
    }
}
2。)SalesController.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebFormsDemo;
using WebFormsDemo.ViewModel;

namespace WebFormsDemo.Controllers
{
public class SalesController : ApiController
{
    private EBMSEntities db = new EBMSEntities();

    // GET: api/Sales
    public IQueryable<SalesViewModel> GetSalesViewModels()
    {
        //return db.SalesViewModels;
        var sales = from order in db.ORDERs
                    join order_detail in db.ORDER_DETAILS
                    on order.ORDER_ID equals order_detail.ORDER_ID
                    join prod in db.PRODUCTs
                    on order_detail.PRODUCT_ID equals prod.PRODUCT_ID
                    orderby order_detail.TOTAL_AMT_PER_ITEM descending
                    //group prod by prod.PRODUCT_CODE
                    group order_detail by prod.PRODUCT_CODE into od
                    select new SalesViewModel
                    {

                        PRODUCT_CODE = od.Key,
                        NET_AMOUNT = od.Sum(p => p.TOTAL_AMT_PER_ITEM).ToString(),

                    };

        return sales;


    }

    // GET: api/Sales/5
    [ResponseType(typeof(SalesViewModel))]
    public IHttpActionResult GetSalesViewModel(int id)
    {
        SalesViewModel salesViewModel = db.SalesViewModels.Find(id);
        if (salesViewModel == null)
        {
            return NotFound();
        }

        return Ok(salesViewModel);
    }

    // PUT: api/Sales/5
    [ResponseType(typeof(void))]
    public IHttpActionResult PutSalesViewModel(int id, SalesViewModel salesViewModel)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != salesViewModel.Id)
        {
            return BadRequest();
        }

        db.Entry(salesViewModel).State = EntityState.Modified;

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!SalesViewModelExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST: api/Sales
    [ResponseType(typeof(SalesViewModel))]
    public IHttpActionResult PostSalesViewModel(SalesViewModel salesViewModel)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.SalesViewModels.Add(salesViewModel);
        db.SaveChanges();

        return CreatedAtRoute("DefaultApi", new { id = salesViewModel.Id }, salesViewModel);
    }

    // DELETE: api/Sales/5
    [ResponseType(typeof(SalesViewModel))]
    public IHttpActionResult DeleteSalesViewModel(int id)
    {
        SalesViewModel salesViewModel = db.SalesViewModels.Find(id);
        if (salesViewModel == null)
        {
            return NotFound();
        }

        db.SalesViewModels.Remove(salesViewModel);
        db.SaveChanges();

        return Ok(salesViewModel);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool SalesViewModelExists(int id)
    {
        return db.SalesViewModels.Count(e => e.Id == id) > 0;
        }
    }
}
2。)SalesVM.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OxyPlot;
using OxyPlot.Series;
using OxyPlot.Xamarin.Forms;
using Xamarin.Forms;
using System.Runtime.CompilerServices;

using XamarinFormsDemo.Models;
using System.Collections.ObjectModel;
using XamarinFormsDemo.Services;

namespace XamarinFormsDemo.ViewModels

{
    public class SalesVM
    {
    private List<Sales> salesmodel { get; set; }


    public List<Sales> SalesModelvm
    {
        get
        {
            return salesmodel;
        }
        set
        {
            salesmodel = value;
            OnPropertyChanged();
        }
    }





    public SalesVM()
    {

        InitializeDataAsync();


    }

    private async Task InitializeDataAsync()
    {
        var salesServices = new SalesServices();
        SalesModelvm = await salesServices.GetSalesAsync();

    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用氧图;
使用OxyPlot.系列;
使用OxyPlot.Xamarin.Forms;
使用Xamarin.Forms;
使用System.Runtime.CompilerServices;
使用XamarinFormsDemo.Models;
使用System.Collections.ObjectModel;
使用XamarinFormsDemo.Services;
命名空间XamarinFormsDemo.ViewModels
{
公共类SalesVM
{
私有列表salesmodel{get;set;}
公共列表SalesModelvm
{
得到
{
退货销售模式;
}
设置
{
销售模式=价值;
OnPropertyChanged();
}
}
公共销售虚拟机()
{
InitializeDataAsync();
}
专用异步任务InitializeDataAsync()
{
var salesServices=新的salesServices();
SalesModelvm=等待salesServices.GetSalesAsync();
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
3。)SalesServices.cs

using Plugin.RestClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;
namespace XamarinFormsDemo.Services
{
    public class SalesServices
    {

    public async Task<List<Sales>> GetSalesAsync()
    {
        RestClient_Sales<Sales> restClient = new RestClient_Sales<Sales>();

        var salesList = await restClient.GetSalesAsync();

        return salesList;

        }

    }
}
使用Plugin.RestClient;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用XamarinFormsDemo.Models;
命名空间XamarinFormsDemo.Services
{
公共类销售服务
{
公共异步任务GetSalesAsync()
{
RestClient_Sales RestClient=新的RestClient_Sales();
var salesList=wait restClient.GetSalesAsync();
退货销售清单;
}
}
}
4。)RestClient.cs

    public class RestClient_Sales<T>
    {

    private const string WebServiceUrl = "http://localhost:50857/api/Sales/";

    public async Task<List<T>> GetSalesAsync()
    {
        var httpClient = new HttpClient();

        var json = await httpClient.GetStringAsync(WebServiceUrl);

        var taskModels = JsonConvert.DeserializeObject<List<T>>(json);

        return taskModels;
       }

    }
public class RestClient\u销售
{
私有常量字符串WebServiceUrl=”http://localhost:50857/api/Sales/";
公共异步任务GetSalesAsync()
{
var httpClient=新的httpClient();
var json=await-httpClient.GetStringAsync(WebServiceUrl);
var taskModels=JsonConvert.DeserializeObject(json);
返回任务模型;
}
}
    public class RestClient_Sales<T>
    {

    private const string WebServiceUrl = "http://localhost:50857/api/Sales/";

    public async Task<List<T>> GetSalesAsync()
    {
        var httpClient = new HttpClient();

        var json = await httpClient.GetStringAsync(WebServiceUrl);

        var taskModels = JsonConvert.DeserializeObject<List<T>>(json);

        return taskModels;
       }

    }