简体   繁体   English

Linq查询以获取Json结果

[英]Linq query to get Json results

I'm trying to do a linq query that will return Json results for a jquery chart. 我正在尝试执行linq查询,该查询将为jquery图表返回json结果。

I have a list of Movement objects: 我有一个运动对象列表:

MovementId
MovedFrom
MovedTo  

So example data would be: 因此示例数据为:

1, Sydney, Melbourne
2, Dallas, Boston
3, Boston, Dallas
4, Boston, Dallas
5, Sydney, Brisbane

I'm trying to get the results like this: 我试图得到这样的结果:

categories: Boston, Brisbane, Dallas, Melbourne, Sydney (note in alphabetical order)
leaving: 2, 0, 1, 0, 2
arriving: 1, 1, 2, 1, 0

So 2 people have left Boston, and 1 has arrived 所以有2人离开了波士顿,其中1人已经抵达

What I've tried so far: 到目前为止,我已经尝试过:

I can get the categories by using the following line query to get the distinct values and merging them: 我可以通过使用以下行查询来获取类别并合并它们来获取类别:

return Json(new
{
    categories = movements
            .Select(m => new[] { m.MovedFrom, m.MovedTo })
            .SelectMany(i => i)
            .Distinct()
            .ToList()
});

However, I can't seem to get an OrderBy working to put these in order. 但是,我似乎无法让OrderBy进行整理。

I haven't found out how to get the leaving and arriving values. 我还没有找到如何获得离开和到达的价值。 Was thinking of a foreach that puts them into an array, but was hoping there was a nice way to do it via linq 曾经考虑过将它们放入数组的foreach,但希望有一种通过linq做到这一点的好方法

I'm not sure if this is what you want. 我不确定这是否是您想要的。 This is not the best approach but can you try this? 这不是最好的方法,但是您可以尝试一下吗?

                        movements
                        .Select(m => new[] { m.MovedFrom, m.MovedTo })
                        .SelectMany(i => i)
                        .Distinct()
                        .OrderBy(e => e)
                        .Select(e => new
                        {
                            categories = e,
                            leaving = movements.Count(x => x.MovedFrom == e),
                            arriving = movements.Count(x => x.MovedTo == e)
                        });

This should do it: 应该这样做:

var cities = movements
    .Select(m => new[] { m.MovedFrom, m.MovedTo })
    .SelectMany(n => n)
    .Distinct()
    .OrderBy(n => n)
    .Select(n => new
    {
        category = n,
        leaving = movements.Count(m => m.MovedFrom == n),
        arriving = movements.Count(m => m.MovedTo == n)
    })
    .ToList();

var result = new
{
    categories = cities.Select(c => c.category).ToList(),
    leaving = cities.Select(c => c.leaving).ToList(),
    arriving = cities.Select(c => c.arriving).ToList()
};

return Json(result);

Fiddle: https://dotnetfiddle.net/eLCcJt 小提琴: https : //dotnetfiddle.net/eLCcJt

I'm not sure if there are any shorter way than this but this will yield you the output you desire. 我不确定是否有比这更短的方法,但这将为您提供所需的输出。 First create a view model class for your chart view. 首先为您的图表视图创建一个视图模型类。

            List<MovementObject> movements = new List<MovementObject>()
            {
                new MovementObject() { MovementId = 1, MovedFrom = "Sydney", MovedTo = "Melbourne"},
                new MovementObject() { MovementId = 2, MovedFrom = "Dallas", MovedTo = "Boston"},
                new MovementObject() { MovementId = 3, MovedFrom = "Boston", MovedTo = "Dallas"},
                new MovementObject() { MovementId = 4, MovedFrom = "Boston", MovedTo = "Dallas"},
                new MovementObject() { MovementId = 5, MovedFrom = "Sydney", MovedTo = "Brisbane"},
            };

            var categories = movements
            .Select(m => new[] { m.MovedFrom, m.MovedTo })
            .SelectMany(i => i)
            .Distinct()
            .ToList();

            MovementChartViewModel viewModel = new MovementChartViewModel();

            foreach(var category in categories.OrderBy(category => category)) //For alphabetical order
            {
                viewModel.Categories.Add(category);
                viewModel.Leaving.Add(movements.Where(move => move.MovedFrom == category).Count());
                viewModel.Arriving.Add(movements.Where(move => move.MovedTo == category).Count());
            }
            return Json(new { viewModel });

Models: 楷模:

        public class MovementChartViewModel
        {
            public MovementChartViewModel()
            {
                Categories = new List<string>();
                Leaving = new List<int>();
                Arriving = new List<int>();
            }

            public List<string> Categories { get; set; }
            public List<int> Leaving { get; set; }
            public List<int> Arriving { get; set; }
        }

        public class MovementObject
        {
            public int MovementId {get;set;}
            public string MovedFrom {get;set;}
            public string MovedTo { get; set; }  
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM