Comparing two table IDs. ASP.NET MVC

miguelbalu

New member
Joined
Dec 14, 2020
Messages
3
Programming Experience
Beginner
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' Erro id

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 error2

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

var color = db.Colors.Where(x => x.ID_Orders = id).ToList();
id query returns a list of ids (even if there is only one id), in next query id is expected to be a single id. If this is what you want to do you can use id.First() to get only first id. If there is multiple ids you could use a Any query.
Also note that comparison operator is ==. A single = is assignment operator.
 
id query returns a list of ids (even if there is only one id), in next query id is expected to be a single id. If this is what you want to do you can use id.First() to get only first id. If there is multiple ids you could use a Any query.
Also note that comparison operator is ==. A single = is assignment operator.
I try this
var id = from d in embOpen
select d.ID_Programa ;
var co = db.Programa_Cor.Where(x => id.Contains(x.ID_Programa)).ToList();

how can I apply All or Any in this situation?
 
I highly doubt that you want to use Any() or Any() considering that you want to get back a list of items.

Any() and Any() take a boolean expression and return a boolean value. You use Any() to see if at least one item in the query is satisfies a condition. You use All() to see if all items in the query satisfy a condition.
 
Where(Contains) is probably what I meant :)
 
C#:
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  };

using join I associate my colors with orders, but list a color and order, color and order, color and order.

How can I list with an join with an order?


I did a little test to list the colors matching the id to value

project view.PNG
 
Back
Top Bottom