简体   繁体   English

比较两个表 ID。 ASP.NET MVC

[英]Comparing two table IDs. ASP.NET MVC

I am currently loading two Orders and Colors tables, I wanted the Colors table to list the items that have the ID equal to Orders.我目前正在加载两个订单和 Colors 表,我希望 Colors 表列出 ID 等于订单的项目。 For this, what occurred to me was to assign the IdOrders values to a variable and compare it with my IdOrders (in my table Colors), but it is not possible to assign the database's balance to my variable为此,我想到的是将 IdOrders 值分配给一个变量并将其与我的 IdOrders (在我的表 Colors 中)进行比较,但是不可能将数据库的余额分配给我的变量

My tables:我的桌子:

public partial class Orders
{
    public int ID_Orders { get; set; }
    public Nullable<System.DateTime> Data_Registo { get; set; }
    public string Num_Encomenda { get; set; }
    public string Ref_Cliente { get; set; }
}

public partial class Colors
{
    public int ID_Orders { get; set; }
    public int ID_Programa_Malha { get; set; }
    public int ID_Linha_Cor { get; set; }
    public string Cor { get; set; }
}

I am working with a database already in operation and possible these tables are already used in a sql join but not how to process that information.我正在使用一个已经在运行的数据库,并且这些表可能已经在 sql 连接中使用,但不是如何处理该信息。

As I said the first thing I remembered was to do this:正如我所说,我记得的第一件事就是这样做:

My Controller:我的 Controller:

var id = from d in db.Orders
         select d.ID_Orders;

        var color = db.Colors.Where(x => x.ID_Orders = id).ToList();

        var tables = new EncomendaViewModel
        {
            Orders= db.Orders.ToList(),
            Colors= color.ToList(),
        };
        return View(tables);

Error in id : CS0029 C# Cannot implicitly convert type to 'int' id错误: CS0029 C# 无法将类型隐式转换为“int” 错误识别码

Is it possible to process the data in this way?是否可以以这种方式处理数据?

Thanks for anyone who can help!感谢任何可以提供帮助的人!

-------------------(Update)------------------------------------------------ - - - - - - - - - -(更新) - - - - - - - - - - - - - - --------------------

Using == cs0019 operator '==' cannot be applied to operands of type使用== cs0019 运算符 '==' 不能应用于类型的操作数错误2

My view in Broswer我在 Broswer 的看法浏览

dbEntities sd = new dbEntities();
        List<Orders> orders= sd.Orders.ToList();
        List<Colors> colers= sd.Colors.ToList();

        var multipletable = from c in orders
                            join st in colers on c.ID_Programa equals st.ID_Programa into table1
                            from st in table1.DefaultIfEmpty()                                
                            select new MultipleClass { orders= c, colers= st  };

There could be one or more values returned from the below query.以下查询可能返回一个或多个值。

var id = from d in db.Orders
         select d.ID_Orders;

That is the reason why it was throwing an error.这就是它抛出错误的原因。 So lets try it this way所以让我们试试这种方式

var color = db.Colors.Where(x => id.Contains(x.ID_Orders)).ToList();
 public class OrderWithColorsViewModel
{
    public Order order { get; set; }
    public List<Colors> colers{ get; set; }
}

Public class TestOrderController : Controller
{
    public DailyMVCDemoContext db = new DailyMVCDemoContext();
    public ActionResult Index()
    {
        var orders= db.Orders.ToList();
        var colers = db.Colors.ToList();
        var result = (from c in orders
                     join st in colers on c.ID_Orders equals st.id into table1
                     select new OrderWithColorsViewModel { order =c, colers = 
                     table1.ToList() }).ToList();
        return View(result);
    }
 }

credits: YihuiSun学分:孙一辉

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

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