1

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 browview

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  };
4
  • Does the error occur in this line ? db.Colors.Where(x => x.ID_Orders = id).ToList(); You need to use "==" incase you are comparing two things -> db.Colors.Where(x => x.ID_Orders == id).ToList(); Commented Dec 11, 2020 at 9:22
  • I'm using only = because if I use == it gives me an error : cs0019 operator '==' cannot be applied to operands of type Commented Dec 11, 2020 at 9:30
  • from d in db.Orders select d.ID_Orders returns the collection instead of single element. If you require an element then select the first (or what ever require) and do the comparison using == Commented Dec 11, 2020 at 9:30
  • using join I can only list the colors ordered, do not list them within the orders because I have more colors, I leave a new scheme above for those who can help. Commented Dec 15, 2020 at 10:27

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

so it works but list all ids, do not list ids equal to Order
In that case , you need to fix your var id = from d in db.Orders select d.ID_Orders; to return only the ID you need.
how can i return the id value of each listing? on top I put a picture of what is being presented to me
0
 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.