How to join two tables using LINQ xamarin.forms

RaniaMKRaniaMK Member ✭✭

i am using xamarin forms with ORM EF i want to show data from product and salesOrderItems tables in datagridView. my problem that the grid is empty.here my code.

public SalesOrderDetailsVM(int salesOrderItemId)
{
_salesOrderItemId = salesOrderItemId;

            _context = new DatabaseContext();

            var salesOrderItem = _context.SalesOrderItems.Find(salesOrderItemId);
            var idsalesOrder = salesOrderItem.SalesOrderId;
            var salesOrder = _context.SalesOrders.Find(idsalesOrder);
            var idProd = salesOrderItem.ProductId;
            _context = new DatabaseContext();

            var Customer = _context.Customers.Where(t => t.CustomerId == salesOrder.CustomerId).FirstOrDefault();
            // Setting property values from customer and salesOrder object
            // that we get from database
            DateCreation = salesOrder.DateCreation;
            NumBc = salesOrder.NumBc;
            CustomerName = Customer.MangerFullName;
            CustomerCIN = Customer.CIN;

             //in this list i want to set  property values from Product and salesOrderItems object
            // that we get from database

            var Products = (from p in _context.Products
                             join o in _context.SalesOrderItems
                             on p.ProductId equals o.ProductId

                             select new Product
                             {
                                 ProductId = p.ProductId,
                                 Code = p.Code,
                                 Designation = p.Designation,
                                 Order_Qty = o.Order_Qty,
                                 PrixUnitaire = p.PrixUnitaire,
                                 TotalHt = o.TotalHt

                             }).ToList();   }
the xaml view :
<ListView ItemsSource="{Binding Products}" 
                          SeparatorVisibility="Default"
                          SelectionMode="None"  
                        >
                    <ListView.ItemTemplate BackgroundColor="DarkSlateGray" >
                        <DataTemplate>
                            <ViewCell>
                                <Frame Padding="5" BorderColor="DarkSlateGray">
                                    <Grid Padding="5" >
                                        <Grid.ColumnDefinitions BorderColor="DarkSlateGray">
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid BackgroundColor="Transparent" >
                                            <Grid.RowDefinitions BorderColor="DarkSlateGray">
                                              <RowDefinition Height="*" />
                                            </Grid.RowDefinitions>
                                        </Grid>

                <!--why  is empty-->

                                        <Label Text="{Binding   Code}" VerticalOptions="Center" Grid.Column="0" />
                                        <Label Text="{Binding Designation}" VerticalOptions="Center" Grid.Column="1" />
                                        <Label  Text="{Binding  Order_Qty}" VerticalOptions="Center" Grid.Column="2" />
                                        <Label Text="{Binding  PrixUnitaire}" VerticalOptions="Center" Grid.Column="3" />
                                        <Label Text="{Binding TotalHt}" VerticalOptions="Center" Grid.Column="4" />
                                    </Grid>
                                </Frame>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>           
 Models

public class SalesOrderItem
{
[Key]
public int SalesOrderItemId { get; set; }
public double TotalHt { get; set; }
public int SalesOrderId { get; set; }
[ForeignKey(("Fk_ SalesOrderId"))]
public virtual SalesOrder SalesOrder { get; set; }
public int ProductId { get; set; }
[ForeignKey(("Fk_ProductId"))]
public virtual Product Product { get; set; }

}

}
public class Product
{
[Key]
public int ProductId { get; set; }
public string Code { get; set; }
public string Designation { get; set; }
public int Quantite { get; set; }
public int Order_Qty { get; set; }
public double PrixUnitaire { get; set; }
public string Type { get; set; }
public double TotalHt { get; set; }

    public ICollection<SalesOrderItem> SalesOrderItems { get; set; }

}
Tagged:

Answers

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited June 28

    Products is declared locally. you should have a public property Products:

    public ObservableCollection<Product> Products { get; set; }
    

    then

    // no var keyword, var is for declaring local properties that can't be accessed outside the method you're in
    Products = new ObservableCollection<Product>((from p in _context.Products     
                                 join o in _context.SalesOrderItems
                                 on p.ProductId equals o.ProductId........ your join query here
    

    No offense, but all your questions seem to indicate that you have extremely basic knowledge of C# (or any other OOP language). I suggest you to take a small pause in your project, and learn C# for a week or two, it will save you lot of troubles in the future.

Sign In or Register to comment.