linq join to entity
-
Wednesday, February 06, 2013 8:28 AM
DataContext objDataContext = new DataContext(ConfigurationManager.ConnectionStrings["MainDB"].ConnectionString);
var MyQuery = from P in objDataContext.GetTable<ProductEntity>()
where P.ProductID == Convert.ToInt32(txtProductID.Text)
join C in objDataContext.GetTable<CustomerEntity>() on P.CustomerID equals C.CustomerID
select new { P.ProductID, P.ProductName, P.ProductCode, C.CustomerName, C.CustomerID };ProductEntity ProductInfo=MyQuery.FIrst<ProductEntity>();
I wrote code Like this.
I also see the value of MyQuery.
But I want to asign the result to my entity(ProductEntity)...
How could I do??
All Replies
-
Wednesday, February 06, 2013 9:15 AM
Replace my written names (ProductID, Name, CustomerName) with correct fields from your ProductEntity class and that's it.select new ProductEntity { ProductID = P.ProductID,
Name = P.ProductName,
CustomerName = C.CustomerName
... };
Please Mark as Reply and Vote as Helpful if I helped.
Also please visit my blog http://msguy.net/ -
Thursday, February 07, 2013 4:07 AM
Hi destar77;
The query MyQuery is NOT returning an instance of ProductEntity but is returning an anonymous type because you are returning selected columns from two tables. By changing the following line of code it should at least return something.
ProductEntity ProductInfo=MyQuery.FIrst<ProductEntity>();
To this
var ProductInfo=MyQuery.FIrst();
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". -
Thursday, February 07, 2013 4:24 AM
I can't do...
I write the code as you shown.
ProductEntity ProductInfo=new ProductEntity();
select new ProductEntity { ProductInfo.ProductID = P.ProductID,
ProductInfo.ProductName = P.ProductName,
ProductInfo.CustomerName = C.CustomerName
... };
I can't what would I do??
-
Thursday, February 07, 2013 4:26 AM
I am not understanding your last post.
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful". -
Thursday, February 07, 2013 7:50 AM
select new ProductInfo { ProductID = P.ProductID, ProductName = P.ProductName, CustomerName = C.CustomerName ... };Please Mark as Reply and Vote as Helpful if I helped.
Also please visit my blog http://msguy.net/

