Add few properties of class in list - c#

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;
}

Related

PetaPoco multi-table join with custom datafield

I used PetaPoco.Compiled 6.0.441...
I want to select data from two tables with inner join ...
always give me error msg
System.InvalidCastException: Object must implement IConvertible.
pls. help me ...
the code as following ..
[TableName("S_SEQ_LOG")]
class S_SEQ_LOG
{
public string gSeqLogId { get; set; }
public string iYear { get; set; }
public string iMonth { get; set; }
public string iDay { get; set; }
public string iCurrentVal { get; set; }
public string vTableNm { get; set; }
[Ignore]
public S_SEQ_RULE ssr { get; set; }
}
[TableName("S_SEQ_RULE")]
class S_SEQ_RULE
{
public string vTableNm { get; set; }
public string vRule { get; set; }
public string vPrefix { get; set; }
public string iLength { get; set; }
public string iCurrentVal { get; set; }
}
var db = DbManager.Create("SqlServer");
var posts = db.Fetch<S_SEQ_LOG, S_SEQ_RULE, S_SEQ_LOG>(
(a, b) =>
{ a.ssr = b; return a; }
,
#"SELECT A.*,B.*
FROM S_SEQ_LOG A INNER JOIN S_SEQ_RULE B ON
A.vTableNm=B.vTableNm
");

Linq To object returns 0 values

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; }
}

Looping with dynamic class

I have this code:
public class TABLE01
{
public string FIELD1 { get; set; }
public string FIELD2 { get; set; }
}
...
// fieldlist:
List<string> fieldsTABLE01 = new List<string>();
fieldsTABLE01.Add("FIELD1");
fieldsTABLE01.Add("FIELD2");
//response type List:
lResponseLegacy.Clear();
lResponseLegacy = DataExtract(fieldsTABLE01);
List<ClassDefinitions.TABLE01> listDataTableTABLE01 = new List<ClassDefinitions.TABLE01>();
foreach (string linea in lResponseLegacy)
{
ClassDefinitions.TABLE01 tableTABLE01 = new ClassDefinitions.TABLE01();
tableTABLE01.table01FIELD1 = ClassPackSupport.GetStringBetween(linea, "<FIELD1>", "</FIELD1>");
tableTABLE01.table01FIELD2 = ClassPackSupport.GetStringBetween(linea, "<FIELD2>", "</FIELD2>");
listDataTableTABLE01.Add(tableTABLE01);
}
classRespuestaDataclassModelo.tableTABLE01 = listDataTableTABLE01;
Now, I need to use the same previous code for others tables like fieldsTABLE01 with their own fields. As well, the previous code must create a list from a dynamic definition of a class (ClassDefinitions.TABLE01) and loop through of their own fields on these class.
In other words, I need to use the same code (maybe on a method) for other classes:
public class TABLE02
{
public string FIELD3 { get; set; }
public string FIELD4 { get; set; }
public string FIELD9 { get; set; }
}
public class TABLE03
{
public string FIELD5 { get; set; }
public string FIELD6 { get; set; }
}
public class TABLE04
{
public string FIELD7 { get; set; }
public string FIELD8 { get; set; }
public string FIELD14 { get; set; }
public string FIELD15 { get; set; }
}
Please, do you give me some tip?
Thanks.
You can use reflection like so
//Get a classes properties, you can do this with many classes.
var yourClassProperties = typeof(YourClass).GetProperties();
//And down the line:
fieldsTABLE01 = fieldsTABLE01.Concat(yourClassProperties).ToList();

Getting the Object From IGrouping in c#

I need to Access available Hotel Object From query2, here I am able to access HotelCode value using y.key, but How Can I Access the availableHotel Object from query2.
My Matrix MOdel
public class JsonMatrixModel
{
public class Result
{
public string responseId { get; set; }
public string searchId { get; set; }
public int totalFound { get; set; }
public List<availableHotels> availableHotels { get; set; }
}
public class availableHotels
{
public string processId { get; set; }
public string hotelCode { get; set; }
public string availabilityStatus { get; set; }
public double totalPrice { get; set; }
public double totalTax { get; set; }
public double totalSalePrice { get; set; }
public string currency { get; set; }
public string boardType { get; set; }
public List<rooms> rooms { get; set; }
}
public class rooms
{
public string roomCategory { get; set; }
public List<paxes> paxes { get; set; }
public double totalRoomRate { get; set; }
public List<ratesPerNight> ratesPerNight { get; set; }
}
public class paxes
{
public string paxType { get; set; }
public int age { get; set; }
}
public class ratesPerNight
{
public string date { get; set; }
public double amount { get; set; }
}
}
My Query
Enumerable<IGrouping<string, JsonMatrixModel.availableHotels>> quer2 =
from ff in ddd
from ss in ff.availableHotels.OrderBy(x =>x.totalSalePrice) group ss by ss.hotelCode;
Accessing the Value
foreach (var y in quer2)
{
string ss = y.Key;
}
After you make the grouping, make a projection to a new anonymous object with a property that will have the value of your key, and another could the the list of grouped values for that key.
var quer2 =
from ff in ddd
from ss in ff.availableHotels.OrderBy(x =>x.totalSalePrice)
group ss by ss.hotelCode
select new
{
GroupKey = ss.Key,
GroupValuesList = ss.ToList()
};
Console.WriteLine(quer2.First().GroupKey);
IGroupping is just an IEnumerable with an additional Key property. is this what you want?
var groups = items.GroupBy(p => p.Property);
foreach (var group in groups)
{
Console.WriteLine(group.Key);
foreach (var item in group)
{
Console.WriteLine("\t{0}", item.AnotherProperty);
}
}

Fill the IList Collection using Entity Framework

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();

Categories