0

下面是我的 ActionMethod 的一部分。我正在努力的部分是使用 db.IntegerBuffers 中的所有 IntegerBufferValues 铸造一个新的变量整数,然后它们添加到db.IntegerList

        var integers = new ICollection<Integers>();

        const int COUNT = 1000000;
        Stopwatch watch = Stopwatch.StartNew();
        for (int c = 0; c < COUNT; c++)
        {
            integers = db.IntegerBuffers.OrderBy(i => i.IntegerBufferValue);
        };
        watch.Stop();

        var integerList = new IntegerList
        {
            Direction = direction,
            Performance = watch.ElapsedMilliseconds,
            Integers = integers
        };

        db.IntegerLists.Add(integerList);

整数列表

namespace Project.Models
{
    public class IntegerList
    {
        public int IntegerListID { get; set; }
        public string Direction { get; set; }
        public long Performance { get; set; }

        public virtual ICollection<Integer> Integers { get; set; }
    }
}

整数缓冲区

namespace Project.Models
{
    public class IntegerBuffer
    {
        public int IntegerBufferID { get; set; }
        public int IntegerBufferValue { get; set; }
    }
}

编辑:显示整数类。

整数

namespace IntegerSorterApp.Models
{
    public class Integer
    {
        public int IntegerID { get; set; }
        public int IntegerValue { get; set; }
        public int IntegerListID { get; set; }

        public virtual IntegerList IntegerList { get; set; }
    }
}
4

1 回答 1

0

integers当前是IntegerBuffer对象的集合。用于.Select将它们投影为整数:

IEnumerable<int> integers;

// ...

integers = db.IntegerBuffers
    .OrderBy(i => i.IntegerBufferValue)
    .Select(i => i.IntegerBufferValue);

或者:

integers = 
    from buffer in db.IntegerBuffers
    orderby buffer.IntegerBufferValue
    select buffer.IntegerBufferValue;

然后,您分配给集合,您可以执行以下操作:

var integerList = new IntegerList
    {
        Direction = direction,
        Performance = watch.ElapsedMilliseconds,
        Integers = integers.ToList()
    };

更新:

哎呀,我没看到那Integer是一堂课。我的答案是创建一个Int32对象列表。基础知识仍然适用:您需要使用 select 来投影到您需要的任何结构。现在我不明白你的数据模型,但也许你想要这样的东西:

// Create your list first so that the list ID is saved
var integerList = new IntegerList
{
    Direction = direction,
    Performance = watch.ElapsedMilliseconds,
};
db.IntegerLists.Add(integerList);


// Now create your Integer records with the ID of the list
foreach (var buffer in db.IntegerBuffers.OrderBy(b => b.IntegerBufferValue))
{
    db.Integers.Add(new Integer
    {
        IntegerValue = buffer.IntegerBufferValue,
        IntegerListID = integerList.IntegerListID
    });
}

如果这是一个一对多的关系,它看起来是,那么IntegerListID在你的Integer记录上设置将隐式地将它添加到Integers列表的集合中。

于 2013-06-03T23:05:00.453 回答