Below is the Entity class
public partial class TestPlanViewModel
{
public TestPlanViewModel()
{
this.TestPlanTestPoints = new List<TestPlanTestPoint>();
}
public int TestPlanId { get; set; }
public string TestPlanName { get; set; }
public virtual IList<TestPlanTestPoint> TestPlanTestPoints { get; set; }
}
public class TestPlanTestPoint
{
public int OrganizationId { get; set; }
public int TestPlanId { get; set; }
public string TestPlanName { get; set; }
public int TestPlanVersion { get; set; }
public int TestPointId { get; set; }
public string TestPointName { get; set; }
}
I need to write a query where I need to get the collections from the dbContext like,
var query = (from tpm in TestPlanMaster
join tptp in TestPlanTestPoint on tpm.TestPlanId equals tptp.TestPlanId
join tmm in TestMethodMaster on tptp.TestMethodId equals tmm.TestMethodId
join tpma in TestPointMaster on tptp.TestPointId equals tpma.TestPointId
select new
{
//Plan Details
tpm.TestPlanId,
tpm.TestPlanName,
}).ToList().Select(x => new Entities.CustomEntities.TestPlanViewModel ====> Custom Entity
{
TestPlanId = x.TestPlanId,
TestPlanName = x.TestPlanName,
TestPlanTestPoints = ?????? ==> how to fill this collection
});
As shown above the TestPlanTestPoints is the IList collection object. I need to populate the data with the values from TestPlanTestPoint table.
Any suggestions?
Model
TestPlan
public partial class TestPlanMaster
{
[DataMember]
public int TestPlanId { get; set; }
[DataMember]
public short TestPlanVersion { get; set; }
[DataMember]
public int OrganizationId { get; set; }
[DataMember]
public short TestPlanTypeId { get; set; }
[DataMember]
public string TestPlanName { get; set; }
}
TestPlanTestPointMapping Model
public partial class TestPlanTestPointMapping
{
[DataMember]
public int OrganizationId { get; set; }
[DataMember]
public int TestPlanId { get; set; }
[DataMember]
public short TestPlanVersion { get; set; }
[DataMember]
public int TestPointId { get; set; }
}
You are thinking too much sql, think more linq. You want to select "tpm" items, so just write a select of those and select the new TestPlanViewModel and fill its properties and navigational properties.
I'm assuming you have a TestPlanTestPoints navigational property in your TestPlanMaster.
var q = (from tpm in TestPlanMaster
select new TestPlanViewModel
{
TestPlanId = tpm.TestPlanId,
TestPlanName = tpm.TestPlanName,
TestPlanPoints = TestPlanTestPoint.Where(pr => tpm.TestPlanId == pr.TestPlanId).Select(tptp => new TestPlanTestPoint()
{
// Fill properties here
}
}).ToList();
Related
I'm getting data from an Oracle database and creating an API for internal use. Some of the classes are (shortened for brevity):
CustomerOrderTab
Public partial class CustomerOrderTab
Public string OrderNo { get; set; }
[ForeignKey("CustomerMV")]
public string CustomerNo { get; set; }
public List<CustomerOrderLineTab> CustomerOrderLines { get; set; }
public List<CustomerOrderChargeTab> CustomerOrderCharge { get; set; }
public List<ShipmentOrderLineTab> ShipmentOrderLines { get; set; }
public CustomerMV CustomerMV { get; set; }
CustomerOrderDTO
Public class OrderDTO
public string OrderNo { get; set; }
public string CustomerName { get; set; }
public List<OrderLinesDTO> OrderLines { get; set; }
public List<OrderChargesDTO> OrderCharges { get; set; }
public List<ShipmentOrderLinesDTO> ShipmentOrderLines { get; set; }
ShipmentOrderLineTab
public partial class ShipmentOrderLineTab
public decimal ShipmentId { get; set; }
[ForeignKey("CustomerOrderTab")]
public string OrderNo { get; set; }
public ShipmentTab Shipment { get; set; }
ShipmentOrderLineDTO
public class ShipmentOrderLinesDTO
public decimal ShipmentId { get; set; }
public string OrderNo { get; set; }
public ShipmentTab MyShipment { get; set; }
ShipmentTab
public partial class ShipmentTab
[ForeignKey("ShipmentOrderLineTab")]
public decimal ShipmentId { get; set; }
public string ShipperAddress1 { get; set; }
public string ShipperAddress2 { get; set; }
public List<ShipmentHandlingUnitTab> ShipmentHandlingUnits { get; set; }
ShipmentDTO
public class ShipmentDTO
public decimal ShipmentId { get; set; }
public string ShipViaCode { get; set; }
public string ShipmentPayer { get; set; }
public List<ShipmentHandlingUnitDTO> ShipmentHandlingUnitDTOs { get; set; }
This Works:
var orderLines = from o in _context.CustomerOrderTab
.Where(o => o.OrderNo == orderno)
select new OrderDTO()
{
OrderNo = o.OrderNo,
CustomerName = o.CustomerMV.CustomerName,
This is a child of the order ShipmentOrderLines = o.ShipmentOrderLines
This works .Select(ob => new ShipmentOrderLinesDTO
{
ShipmentId = ob.ShipmentId,
OrderNo = ob.OrderNo,
MyShipment = ob.Shipment
Adding this does not work .Select(y => new ShipmentDTO
The error says that ShipmentTab
Doesn’t have a definition for Select {
}
}).ToList()
Shipment is a child of ShipmentOrderLine (there will only be one shipment per line)
ShipmentOrderLine is a child of CustomerOrder (there can be many lines per order)
I think the problem is that there is only one shipment per line but I've tried lots of things and can't get it to map to my DTO.
Fix your model:
ShipmentOrderLineDTO
public class ShipmentOrderLinesDTO
public decimal ShipmentId { get; set; }
public string OrderNo { get; set; }
public ShipmentDTO MyShipment { get; set; }
Select is only used to project a collection. For non-collections, you just create a new object...
var orderLines = _context.CustomerOrderTab
.Where(o => o.OrderNo == orderno)
.Select(o => new OrderDTO {
OrderNo = o.OrderNo,
CustomerName = o.CustomerMV.CustomerName,
ShipmentOrderLines = o.ShipmentOrderLines
.Select(ob => new ShipmentOrderLinesDTO {
ShipmentId = ob.ShipmentId,
OrderNo = ob.OrderNo,
MyShipment = new ShipmentDTO {
ShipmentId = ob.Shipment.ShipmentId,
...
ShipmentHandlingUnits = ob.Shipment.ShipmentHandlingUnits
.Select(shu=> new ShipmentHandlingUnitDTO {
...
}).ToList()
}
}
}).ToList()
Now with that all said, I typically have mapping extension methods on my data model classes, which allow things like this:
var orderLines = _context.CustomerOrderTab
.Where(o=>o.OrderNo == orderno)
.Select(o=> o.ToDto());
Example class:
public static class CustomerOrderTabExtensions {
public static CustomerOrderDto ToDto(this CustomerOrderTab cot) {
return new CustomerOrderDto {
OrderNo = cot.OrderNo,
...
OrderLines = cot.CustomerOrderLines
.Select(col=>col.ToDto())
.ToList()
}
}
}
Or use Automapper.
I'm trying to get some data from to Lists using Linq. If I use join with equals using one field in each list it's ok and returns number of records as expected, but if I try use join on pair of fields it returns 0 records. Why?
List<Result> inner_join =
(from egais in rests_egais
//join best in rests_best on egais.Product.AlcCode equals best.Product.AlcCode
join best in rests_best
on new {key1 = egais.Shop, key2 = egais.Product.AlcCode}
equals new { key1 = best.Shop, key2 = best.Product.AlcCode }
where egais.Quantity != best.Quantity
select new Result
{
Shop = egais.Shop,
AlcCode = egais.Product.AlcCode,
FullName = egais.Product.FullName,
Quantity_Egais = egais.Quantity,
Quantity_Best = best.Quantity,
Difference = best.Quantity - egais.Quantity
}).ToList();
EDITED: These are classes to use:
public class Rest
{
public Rest()
{
Product = new Product();
}
public string Shop { get; set; }
public DateTime Date { get; set; }
public double Quantity { get; set; }
public string InformF1RegId { get; set; }
public string InformF2RegId { get; set; }
public Product Product { get; set; }
}
public class Product
{
public Product()
{
Producer = new Producer();
}
public string FullName { get; set; }
public string AlcCode { get; set; }
public double Capacity { get; set; }
public string UnitType { get; set; }
public double AlcVolume { get; set; }
public int ProductVCode { get; set; }
public Producer Producer { get; set; }
}
public class Producer
{
public string ClientRegId { get; set; }
public string FullName { get; set; }
public string ShortName { get; set; }
public int Country { get; set; }
public string Description { get; set; }
}
public class Result
{
public string Shop { get; set; }
public string AlcCode { get; set; }
public string FullName { get; set; }
public double Quantity_Egais { get; set; }
public double Quantity_Best { get; set; }
public double Difference { get; set; }
}
I have a Model:
public class Transaction
{
public Guid ID { get; set; }
public Guid CategoryID { get; set; }
public List<Guid> Tags { get; set; }
}
public class Category
{
public Guid ID { get; set; }
public string Caption { get; set; }
}
public class Tag
{
public Guid ID { get; set; }
public string Caption { get; set; }
}
I have a ModelRepository of Model objects. I have VieModels:
public class TransactionViewModel
{
public Guid ID { get; set; }
public CategoryViewModel Category { get; set; }
public List<TagViewModel> Tags { get; set; }
}
public class CategoryViewModel
{
public Guid ID { get; set; }
public string Caption { get; set; }
}
public class TagViewModel
{
public Guid ID { get; set; }
public string Caption { get; set; }
}
Now I need to make List<TransactionViewModel> from List <Transaction> using linq. How did I:
VM.Categories = ModelRepository.Categories.Select(p => new CategoryViewModel(p)));
VM.Tags = ModelRepository.Tags.Select(p => new TagViewModel(p)));
var trans = from t in ModelRepository.Transactions
join c in VM.Categories on t.CategoryID equals c.ID
select new TransactionViewModel()
{
...,
Category = c
};
VM.Transactions = trans.ToList();
Now when I change any CategoryViewModel in VM.Categories, I immediately get changes for all TransactionViewModel.Category in VM.Transactions. Question - how do I fill TransactionViewModel.Tags? Preferably with LINQ. And it's mandatory that when changing any TagViewModel in VM.Tags, I immediately get changes for all TransactionViewModel.Tags in VM.Transactions.
I have a dbcontext with a dbset, and I need to make a linq query easily.
using (ZigBeeContext db = new ZigBeeContext())
{
var lista = (from p in db.Medidas
select new Medida
{
Fecha = p.FechaHora,
}).ToList();
}
However I get the error on the title on db.Medidas
Medidas.cs
public class Medida
{
[Key]
[DisplayName("Medida")]
public int MedidaID { get; set; }
public decimal Temperatura { get; set; }
public int Humedad { get; set; }
public DateTime FechaHora { get; set; }
[DisplayName("Dispositivo")]
public int DispositivoID { get; set; }
public virtual Dispositivo Dispositivo { get; set; }
}
I have a class with 8 properties declared:
public class classProduct
{
public int iProductID { get; set; }
public string sPName { get; set; }
public string sPDescription { get; set; }
public int iPTypeID { get; set; }
public string sPPrice { get; set; }
public string sPType { get; set; }
public string sImagePath { get; set; }
public string sDestinationPath { get; set; }
}
And I have a method in which I add 4 properties in my list like and ProductDetailsTbls is my Tbl1 and ProductTypeTbls is Tbl2.
public List<classProduct> GetAllProduct()
{
List<classProduct> lst = new List<classProduct>();
lst = (from c in dc.ProductDetailsTbls
join d in dc.ProductTypeTbls
on c.PTypeID equals d.PTypeID
select new classProduct { sPName=c.PName, sPDescription=c.PDescription, sPPrice=c.PPrice, sPType=d.PType }).ToList();
return lst;
}
But it returns all properties that I don't want.
If you only want some properties I think you will have to introduce a new class.
My approach for this would be to define a base class with your four properties and then you can classProduct inherit from it.
your base-class then looks like:
public class baseClassProduct
{
public string sPName { get; set; }
public string sPDescription { get; set; }
public string sPPrice { get; set; }
public string sPType { get; set; }
}
and your classProduct:
public class classProduct : baseClassProduct
{
public int iProductID { get; set; }
public int iPTypeID { get; set; }
public string sImagePath { get; set; }
public string sDestinationPath { get; set; }
}
In your GetAllProduct-Method you can work with the baseClass and you'll get only the information you want to.
public List<baseClassProduct> GetAllProduct()
{
List<baseClassProduct> lst = new List<baseClassProduct>();
lst = (from c in dc.ProductDetailsTbls
join d in dc.ProductTypeTbls
on c.PTypeID equals d.PTypeID
select new baseClassProduct { sPName=c.PName, sPDescription=c.PDescription, sPPrice=c.PPrice, sPType=d.PType }).ToList();
return lst;
}