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. 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

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.

As I said the first thing I remembered was to do this:

My 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’
Comparing two table IDs. ASP.NET MVC

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
Comparing two table IDs. ASP.NET MVC

My view in Broswer
Comparing two table IDs. ASP.NET MVC

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

 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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x