0

I have 2 tables in the database - Customer and Product. I perform the selection by skipping the first row in the table and then getting the top two rows. The result I need to get in the List cusPod .

List<Customer> customers = db.Database.SqlQuery<Customer>("SELECT * FROM Customers ORDER BY CustomerId OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY").ToList();
List<Product> products = db.Database.SqlQuery<Product>("SELECT * FROM Products ORDER BY ProductId OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY").ToList();
            var listSas = from p in products
                          join c in customers on p.ProductId equals c.CustomerId
                          select new { ProductId = p.ProductId, ProductName = p.ProductName, DateStart = p.DateStart, DateEnd = p.DateEnd, DateRegister = p.DateRegister, PriceCustomer = p.PriceCustomer, CheckPay = p.CheckPay, CustomerId = p.CustomerId, FIO = c.FIO, Email = c.Email, PhoneNumber = c.PhoneNumber };
            SidebarController.cusPod = listSas.ToList();

how do I do this right?

3uk Mat
  • 43
  • 3

1 Answers1

0

From the code it seems that you are using entity framework as your ORM, therefore, you can easily use the Skip and Take methods of EF that are great features instead of writing hard coded sql. for example:

db.Customers.OrderBy(c => c.CustomerId).Skip(1).Take(2);

The rest of your logic seems fine.

Amir Molaei
  • 3,700
  • 1
  • 17
  • 20