Linq查询获取Json结果

时间:2016-03-02 01:49:24

标签: c# asp.net linq

我正在尝试执行linq查询,该查询将返回json结果的json结果。

我有一个Movement对象列表:

MovementId
MovedFrom
MovedTo  

所以示例数据将是:

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

我试图得到这样的结果:

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

所以有2人离开波士顿,1人已经到了

到目前为止我已尝试过:

我可以使用以下行查询获取类别以获取不同的值并合并它们:

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

但是,我似乎无法让OrderBy努力将这些按顺序排列。

我还没有发现如何获得离开和到达的价值观。想到了一个将它们放入数组的foreach,但希望通过linq有一个很好的方法来实现它

3 个答案:

答案 0 :(得分:1)

我不确定这是不是你想要的。这不是最好的方法,但你能尝试一下吗?

                        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)
                        });

答案 1 :(得分:1)

这应该这样做:

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);

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

答案 2 :(得分:0)

我不确定是否有比这更短的方法,但这会产生你想要的输出。首先为图表视图创建一个视图模型类。

            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 });

型号:

        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; }  
        }