如何迭代未知数量的模型并将该模型中的数据与控制器中的另一个模型进行比较?

时间:2022-02-06 06:27:15

So here's the situation. I am new to MVC and I am looking for the simplest way to go about Iterating through an unknown number of one model to continually look for the highest integer value of one property of that model based on data from another model.

所以这就是情况。我是MVC的新手,我正在寻找最简单的方法来迭代未知数量的一个模型,以便根据另一个模型的数据不断寻找该模型的一个属性的最高整数值。

To give you a better idea, here's some background:
I have an application set up to allow a user to choose 1, 2, 3, or 4. Once a number has been chosen, the application must then search through an unknown number of models which have different integer values for 1, 2, 3, 4. Model one might have 6, 8, 7, 2 and model two might have 9, 7, 6, 3. I need to first read in the 1, 2, 3, or 4 from the first model and then based on that selection (let's say 2) examine each instance of the other model for column 2 looking for the highest value (in this example that would be 8 coming from the second model's second example).

为了给你一个更好的主意,这里有一些背景知识:我有一个应用程序设置允许用户选择1,2,3或4.一旦选择了一个数字,应用程序必须搜索未知数量的模型它有1,2,3,4的不同整数值。模型一可能有6,8,7,2,模型二可能有9,7,6,3。我需要先读入1,2,3 ,或者来自第一个模型的4个,然后基于该选择(假设2)检查第2列的另一个模型的每个实例,寻找最高值(在这个例子中,来自第二个模型的第二个例子是8)。

In this example, this is the first model which determines which column will be examined:

在此示例中,这是第一个确定要检查哪个列的模型:

public class Input
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool GreenCircle { get; set; }
    public bool BlueSquare { get; set; }
    public bool BlackDiamond { get; set; }
    public bool TerrainPark { get; set; }
}

This is the second model which will have an unknown number of them as I want the ability to add and delete Resorts at any time:

这是第二个具有未知数量的模型,因为我希望能够随时添加和删除Resorts:

public class Resort
{
    public int ID { get; set; }
    public String Name { get; set; }
    public int BlackDiamond { get; set; }
    public int BlueSquare { get; set; }
    public int GreenCircle { get; set; }
    public int TerrainPark { get; set; }
}

So a user will choose BlackDiamond, then the application must search through each Resort Model to find the one with the highest value in the BlackDiamond property.

因此,用户将选择BlackDiamond,然后应用程序必须搜索每个度假村模型以找到BlackDiamond属性中具有最高值的那个。

My idea so far was to have a controller that has the DBContext for both models but I do not know how to properly iterate through an unknown number of the second model. Any ideas?

到目前为止,我的想法是让控制器具有两个模型的DBContext,但我不知道如何正确地迭代未知数量的第二个模型。有任何想法吗?

2 个解决方案

#1


1  

Similar to @Paul's answer, but using your classes:

类似于@Paul的答案,但使用你的课程:

List<Resort> resorts = GetResortsFromDatabase();
if (input.BlackDiamond)
{
    return resorts.OrderByDescending(x => x.BlackDiamond).First();
}
else if (input.BlueSquare)
{
    return resorts.OrderByDescending(x => x.BlueSquare).First();
}
// etc...

If you need more complicated logic, you could instead use a foreach loop:

如果你需要更复杂的逻辑,你可以使用foreach循环:

foreach(var resort in GetResortsFromDatabase())
{
    if (input.BlackDiamond && resort.BlackDiamond > 5) 
    {
        DoSomething(input, resort); // This will do something to the input and each resort.
    }
}

If you need help writing the GetResortsFromDatabase() function, then you need to ask a question like "How do I get multiple records from my database" and tell us how you're talking to the database. (Linq2Sql? Entity Framework? Something else?).

如果您需要编写GetResortsFromDatabase()函数的帮助,那么您需要问一个问题,如“如何从我的数据库中获取多条记录”,并告诉我们您是如何与数据库通信的。 (Linq2Sql?实体框架?还有什么?)。

#2


0  

I'm sure others will do better, but could something like this fit the bill?

我相信其他人会做得更好,但是这样的话能适合这个账单吗?

class MyModel
{
    public int[] PropertyValues = new int[4];
}

class Main
{
    IEnumerable<MyModel> models;

    public void MyFunc()
    {
        var models = new List<MyModel>();

        models.Add(new MyModel() { PropertyValues = new int[] { 6, 8, 7, 2 } });
        models.Add(new MyModel() { PropertyValues = new int[] { 9, 7, 6, 3 } });

        var index = 1;

        var maxValue = models.Max(m => m.PropertyValues[1]);
    }
}

#1


1  

Similar to @Paul's answer, but using your classes:

类似于@Paul的答案,但使用你的课程:

List<Resort> resorts = GetResortsFromDatabase();
if (input.BlackDiamond)
{
    return resorts.OrderByDescending(x => x.BlackDiamond).First();
}
else if (input.BlueSquare)
{
    return resorts.OrderByDescending(x => x.BlueSquare).First();
}
// etc...

If you need more complicated logic, you could instead use a foreach loop:

如果你需要更复杂的逻辑,你可以使用foreach循环:

foreach(var resort in GetResortsFromDatabase())
{
    if (input.BlackDiamond && resort.BlackDiamond > 5) 
    {
        DoSomething(input, resort); // This will do something to the input and each resort.
    }
}

If you need help writing the GetResortsFromDatabase() function, then you need to ask a question like "How do I get multiple records from my database" and tell us how you're talking to the database. (Linq2Sql? Entity Framework? Something else?).

如果您需要编写GetResortsFromDatabase()函数的帮助,那么您需要问一个问题,如“如何从我的数据库中获取多条记录”,并告诉我们您是如何与数据库通信的。 (Linq2Sql?实体框架?还有什么?)。

#2


0  

I'm sure others will do better, but could something like this fit the bill?

我相信其他人会做得更好,但是这样的话能适合这个账单吗?

class MyModel
{
    public int[] PropertyValues = new int[4];
}

class Main
{
    IEnumerable<MyModel> models;

    public void MyFunc()
    {
        var models = new List<MyModel>();

        models.Add(new MyModel() { PropertyValues = new int[] { 6, 8, 7, 2 } });
        models.Add(new MyModel() { PropertyValues = new int[] { 9, 7, 6, 3 } });

        var index = 1;

        var maxValue = models.Max(m => m.PropertyValues[1]);
    }
}