I want to update two tables in single query in c# - c#

public bool UpdateLevelDetailsList(List<LevelDetailsDALDTO> LevelDetailsObjToUpdate)
{
bool result = false;
//List<LevelDetail> editLevelDetails = DBContext.LevelDetails.Where(g => g.LevelID == a.LevelID).ToList();
List<LevelDetail> levelDetails = DBContext.LevelDetails.ToList();
List<VisioShapeData> VisioData = DBContext.VisioShapeDatas.ToList();
foreach (LevelDetailsDALDTO a in LevelDetailsObjToUpdate)
{
//List<LevelDetail> editLevelDetails = DBContext.LevelDetails.Where(g => g.LevelID == a.LevelID).ToList();
List<LevelDetail> editLevelDetails = levelDetails.Where(x => x.LevelID == a.LevelID).ToList();
List<VisioShapeData> updatevisiodata = VisioData.Where(x => x.ID == a.ID).ToList();
if (a.CreatedDate == DateTime.MinValue)
a.CreatedDate = DateTime.Now;
if (a.ModifiedDate == DateTime.MinValue)
a.ModifiedDate = DateTime.Now;
if (editLevelDetails != null)
{
foreach (LevelDetail b in editLevelDetails)
{
b.Cost += a.Cost;
b.Time += a.Time;
b.Responsible = b.Responsible + "," + a.Responsible;
b.ModifiedBy = a.ModifiedBy;
b.ModifiedDate = a.ModifiedDate;
}
if (updatevisiodata != null)
{
foreach (VisioShapeData c in updatevisiodata)
{
c.Level4Id = a.LevelID;
}
}
if (DBContext.SaveChanges() > 0)
result = true;
}
}
return result;
}
want to combine these two statement in
List<LevelDetail> editLevelDetails = levelDetails.Where(x => x.LevelID == a.LevelID).ToList();
List<VisioShapeData> updatevisiodata = VisioData.Where(x => x.ID == a.ID).ToList();
and

Related

Acumatica: Ability to override DefaultEndpointImpl to add advanced custom logic to contract REST API

I'm wondering if it possible to override DefaultEndpointImpl.cs or add my own API logic in another file?
I'm struggling with a few API calls which require the logic in this file to be overridden or added to. For example I am able to create a purchase receipt for a PO via the api successfully, however I'm not able to add a "Transfer Receipt" purchase receipt in the same way.
I've included various non-standard fields to the API endpoints that reference the original transfer, transfer order and shipment but have been unsuccessful. The API calls succeeds but no lines are added. I've been able to get lines to update their quantity once generated but can't add or delete current lines.
The problem looks to stem from this code, which seems to add functionality that correctly handles adding Purchase order lines logic of the graph but doesn't do anything special for other receipt types.
[FieldsProcessed(new[] { "POLineNbr", "POOrderType", "POOrderNbr" })]
protected void PurchaseReceiptDetail_Insert(PXGraph graph, EntityImpl entity, EntityImpl targetEntity) {
var receiptEntry = (POReceiptEntry)graph;
var lineNbr = targetEntity.Fields.SingleOrDefault(f => f.Name == "POLineNbr") as EntityValueField;
var orderType = targetEntity.Fields.SingleOrDefault(f => f.Name == "POOrderType") as EntityValueField;
var orderNbr = targetEntity.Fields.SingleOrDefault(f => f.Name == "POOrderNbr") as EntityValueField;
bool insertViaAddPO = lineNbr != null && orderNbr != null && orderType != null;
if (!insertViaAddPO && (lineNbr != null || orderType != null || orderNbr != null)) {
throw new PXException(PO.Messages.POTypeNbrLineNbrMustBeFilled);
}
var detailsCache = receiptEntry.transactions.Cache;
if (insertViaAddPO) {
receiptEntry.filter.Cache.Remove(receiptEntry.filter.Current);
receiptEntry.filter.Cache.Insert(new POReceiptEntry.POOrderFilter());
var filter = receiptEntry.filter.Current; var state = receiptEntry.filter.Cache.GetStateExt(filter, "OrderType") as PXStringState;
if (state != null && state.AllowedLabels.Contains(orderType.Value)) {
orderType.Value = state.ValueLabelDic.Single(p => p.Value == orderType.Value).Key;
}
receiptEntry.filter.Cache.SetValueExt(filter, "OrderType", orderType.Value);
receiptEntry.filter.Cache.SetValueExt(filter, "OrderNbr", orderNbr.Value);
receiptEntry.filter.Update(filter);
var orders = receiptEntry.poLinesSelection.Select().Select(r => r.GetItem<POReceiptEntry.POLineS>());
var order = orders.FirstOrDefault(o => o.LineNbr == int.Parse(lineNbr.Value));
if (order == null) {
throw new PXException(PO.Messages.PurchaseOrderLineNotFound);
}
order.Selected = true;
receiptEntry.poLinesSelection.Update(order);
receiptEntry.Actions["AddPOOrderLine2"].Press();
} else {
detailsCache.Current = detailsCache.Insert();
}
}
After a lot of trial and error I have managed to get everything working as I need. However I'm concerned that since this is not documented information that it may be subject to change in the near future.
The methods in the class DefaultEndpointImpl are called via reflection. To add custom functionality for my default endpoint I have extended the default class and added the necessary attributes to my class and functions.
My custom class adds the following:
Overides the default PurchaseReceiptDetail_Insert function to add transfer receipt functionality.
Adds ReceiptDetail_Insert method to allow adding an IN receipt referencing
The first one was pretty straight forward and I'm happy with the outcome. The second one took a lot of time and debugging to get right, is hacky and probably doesn't work with serial tracked items. The reason it's taken so long is because there are many unknowns and I've essentially been guessing based on the code in the base class. For example I'm not even sure at which stage this function fires. It looks the functions completely override some other default logic which is invisible.
So take it or leave it! Maybe I will reach out to Acumatica to see if I can get more information.
using PX.Api;
using PX.Api.ContractBased;
using PX.Api.ContractBased.Models;
using PX.Data;
using PX.Objects.PO;
using PX.Objects.IN;
using System;
using System.Linq;
using PX.Objects.CM;
using PX.Objects.CS;
namespace AcuStock
{
[PXVersion("5.30.001", "AcuStock")]
[PXVersion("6.00.001", "AcuStock")]
[PXVersion("17.200.001", "AcuStock")]
public class AcuStockEndpointImpl : PX.Objects.DefaultEndpointImpl
{
[FieldsProcessed(new[] { "POLineNbr", "POOrderType", "POOrderNbr", "OrigLineNbr", "OrigRefNbr" })]
protected new void PurchaseReceiptDetail_Insert(PXGraph graph, EntityImpl entity, EntityImpl targetEntity){
var lineNbr = targetEntity.Fields.SingleOrDefault(f => f.Name == "OrigLineNbr") as EntityValueField;
var refNbr = targetEntity.Fields.SingleOrDefault(f => f.Name == "OrigRefNbr") as EntityValueField;
var receiptQty = targetEntity.Fields.SingleOrDefault(f => f.Name == "ReceiptQty") as EntityValueField;
var location = targetEntity.Fields.SingleOrDefault(f => f.Name == "Location") as EntityValueField;
var inventoryID = targetEntity.Fields.SingleOrDefault(f => f.Name == "InventoryID") as EntityValueField;
bool insertViaAddTR = lineNbr != null && refNbr != null;
var receiptEntry = (POReceiptEntry) graph;
if (insertViaAddTR){
receiptEntry.addReceipt.Cache.Remove(receiptEntry.addReceipt.Current);
receiptEntry.addReceipt.Cache.Insert(new POReceiptEntry.POReceiptLineS());
var filter = receiptEntry.addReceipt.Current;
receiptEntry.addReceipt.Cache.SetValueExt(filter, "OrigRefNbr", refNbr.Value);
receiptEntry.addReceipt.Cache.SetValueExt(filter, "OrigLineNbr", lineNbr.Value);
receiptEntry.addReceipt.Cache.SetValueExt(filter, "InventoryID", inventoryID.Value);
receiptEntry.addReceipt.Cache.SetValueExt(filter, "ReceiptQty", receiptQty.Value);
if (location != null)
receiptEntry.addReceipt.Cache.SetValueExt(filter, "LocationID", location.Value);
receiptEntry.addReceipt.Update(filter);
var lines = receiptEntry.addReceipt.Select().Select(r => r.GetItem<POReceiptEntry.POReceiptLineS>());
var line = lines.FirstOrDefault(o => o.OrigLineNbr == int.Parse(lineNbr.Value));
if (line == null){
throw new PXException(PX.Objects.PO.Messages.PurchaseOrderLineNotFound);
}
receiptEntry.addPOReceiptLine2.Press();
} else {
base.PurchaseReceiptDetail_Insert(graph, entity, targetEntity);
}
var allocations = (targetEntity.Fields.SingleOrDefault(f => string.Equals(f.Name, "Allocations")) as EntityListField).Value ?? new EntityImpl[0];
if (allocations.Any(a => a.Fields != null && a.Fields.Length > 0)){
// Remove automatically added allocation
if (receiptEntry.splits.Current != null){
receiptEntry.splits.Delete(receiptEntry.splits.Current);
}
}
}
[FieldsProcessed(new[] { "OrigLineNbr", "OrigRefNbr", "Quantity", "Location" })]
protected void ReceiptDetails_Insert(PXGraph graph, EntityImpl entity, EntityImpl targetEntity) {
var lineNbr = targetEntity.Fields.SingleOrDefault(f => f.Name == "OrigLineNbr") as EntityValueField;
var receiptQty = targetEntity.Fields.SingleOrDefault(f => f.Name == "Quantity") as EntityValueField;
var location = targetEntity.Fields.SingleOrDefault(f => f.Name == "Location") as EntityValueField;
var allocations = (targetEntity.Fields.SingleOrDefault(f => string.Equals(f.Name, "Allocations")) as EntityListField).Value ?? new EntityImpl[0];
var hasAllocations = allocations.Any(a => a.Fields != null && a.Fields.Length > 0);
var receiptEntry = (INReceiptEntry) graph;
string transferNbr = receiptEntry.receipt.Current.TransferNbr;
var detailsCache = receiptEntry.transactions.Cache;
if (lineNbr == null || transferNbr == null){
detailsCache.Current = detailsCache.Insert();
return;
}
INTran newtran = null;
decimal newtranqty = Decimal.Parse(receiptQty.Value);
decimal newtrancost = 0m;
receiptEntry.ParseSubItemSegKeys();
using (new PXReadBranchRestrictedScope())
{
foreach (PXResult<INTransitLine, INLocationStatus2, INTransitLineLotSerialStatus, INSite, InventoryItem, INTran> res in
PXSelectJoin<INTransitLine,
InnerJoin<INLocationStatus2, On<INLocationStatus2.locationID, Equal<INTransitLine.costSiteID>>,
LeftJoin<INTransitLineLotSerialStatus,
On<INTransitLine.transferNbr, Equal<INTransitLineLotSerialStatus.transferNbr>,
And<INTransitLine.transferLineNbr, Equal<INTransitLineLotSerialStatus.transferLineNbr>>>,
InnerJoin<INSite, On<INSite.siteID, Equal<INTransitLine.toSiteID>>,
InnerJoin<InventoryItem, On<InventoryItem.inventoryID, Equal<INLocationStatus2.inventoryID>>,
InnerJoin<INTran,
On<INTran.docType, Equal<INDocType.transfer>,
And<INTran.refNbr, Equal<INTransitLine.transferNbr>,
And<INTran.lineNbr, Equal<INTransitLine.transferLineNbr>,
And<INTran.invtMult, Equal<shortMinus1>>>>>>>>>>,
Where<INTransitLine.transferNbr, Equal<Required<INTransitLine.transferNbr>>, And<INTransitLine.transferLineNbr, Equal<Required<INTransitLine.transferLineNbr>>>>,
OrderBy<Asc<INTransitLine.transferNbr, Asc<INTransitLine.transferLineNbr>>>>
.Select(receiptEntry, transferNbr, lineNbr.Value))
{
INTransitLine transitline = res;
INLocationStatus2 stat = res;
INTransitLineLotSerialStatus lotstat = res;
INSite site = res;
InventoryItem item = res;
INTran tran = res;
if (stat.QtyOnHand == 0m || (lotstat != null && lotstat.QtyOnHand == 0m))
continue;
if (newtran == null) {
if (!object.Equals(receiptEntry.receipt.Current.BranchID, site.BranchID))
{
INRegister copy = PXCache<INRegister>.CreateCopy(receiptEntry.receipt.Current);
copy.BranchID = site.BranchID;
receiptEntry.receipt.Update(copy);
}
newtran = PXCache<INTran>.CreateCopy(tran);
newtran.OrigBranchID = newtran.BranchID;
newtran.OrigTranType = newtran.TranType;
newtran.OrigRefNbr = transitline.TransferNbr;
newtran.OrigLineNbr = transitline.TransferLineNbr;
newtran.BranchID = site.BranchID;
newtran.DocType = receiptEntry.receipt.Current.DocType;
newtran.RefNbr = receiptEntry.receipt.Current.RefNbr;
newtran.LineNbr = (int)PXLineNbrAttribute.NewLineNbr<INTran.lineNbr>(receiptEntry.transactions.Cache, receiptEntry.receipt.Current);
newtran.InvtMult = (short)1;
newtran.SiteID = transitline.ToSiteID;
newtran.LocationID = transitline.ToLocationID;
newtran.ToSiteID = null;
newtran.ToLocationID = null;
newtran.BaseQty = 0m;
newtran.Qty = 0m;
newtran.UnitCost = 0m;
newtran.Released = false;
newtran.InvtAcctID = null;
newtran.InvtSubID = null;
newtran.ReasonCode = null;
newtran.ARDocType = null;
newtran.ARRefNbr = null;
newtran.ARLineNbr = null;
newtran.ProjectID = null;
newtran.TaskID = null;
newtran.CostCodeID = null;
newtran.TranCost = 0m;
receiptEntry.splits.Current = null;
newtran = receiptEntry.transactions.Insert(newtran);
receiptEntry.transactions.Current = newtran;
if (receiptEntry.splits.Current != null)
{
receiptEntry.splits.Delete(receiptEntry.splits.Current);
}
}
if (hasAllocations){
newtranqty = 0m;
foreach (var allocation in allocations) {
var newsplitqty = allocation.Fields.SingleOrDefault(f => f.Name == "Quantity") as EntityValueField;
var newsplitlocation = allocation.Fields.SingleOrDefault(f => f.Name == "Location") as EntityValueField;
INTranSplit newsplit = this.addReceiptSplitLine(receiptEntry, stat, lotstat, transitline, newtran, item, Decimal.Parse(newsplitqty.Value), newsplitlocation.Value);
newtrancost += newsplit.BaseQty.Value * newsplit.UnitCost.Value;
newtranqty += newsplit.BaseQty.Value;
}
break;
} else {
INTranSplit newsplit = this.addReceiptSplitLine(receiptEntry, stat, lotstat, transitline, tran, item, newtranqty, null);
newtrancost += newsplit.BaseQty.Value * newsplit.UnitCost.Value;
newtranqty += newsplit.BaseQty.Value;
}
}
receiptEntry.UpdateTranCostQty(newtran, newtranqty, newtrancost);
}
}
[FieldsProcessed(new[] { "OrigLineNbr" })]
protected void ReceiptAllocations_Insert(PXGraph graph, EntityImpl entity, EntityImpl targetEntity) {
// no-op
}
private INTranSplit addReceiptSplitLine(INReceiptEntry receiptEntry, INLocationStatus2 stat, INTransitLineLotSerialStatus lotstat, INTransitLine transitline, INTran tran, InventoryItem item, Decimal qty, string location){
INTranSplit newsplit;
decimal newsplitqty;
if (lotstat.QtyOnHand == null)
{
newsplit = new INTranSplit();
newsplit.InventoryID = stat.InventoryID;
newsplit.IsStockItem = true;
newsplit.FromSiteID = transitline.SiteID;
newsplit.SubItemID = stat.SubItemID;
newsplit.LotSerialNbr = null;
newsplitqty = qty;
}
else
{
newsplit = new INTranSplit();
newsplit.InventoryID = lotstat.InventoryID;
newsplit.IsStockItem = true;
newsplit.FromSiteID = lotstat.FromSiteID;
newsplit.SubItemID = lotstat.SubItemID;
newsplit.LotSerialNbr = lotstat.LotSerialNbr;
newsplitqty = qty;
}
newsplit.DocType = receiptEntry.receipt.Current.DocType;
newsplit.RefNbr = receiptEntry.receipt.Current.RefNbr;
newsplit.LineNbr = tran.LineNbr;
newsplit.SplitLineNbr = (int)PXLineNbrAttribute.NewLineNbr<INTranSplit.splitLineNbr>(receiptEntry.splits.Cache, receiptEntry.receipt.Current);
newsplit.UnitCost = 0m;
newsplit.InvtMult = (short)1;
newsplit.SiteID = transitline.ToSiteID;
newsplit.PlanID = null;
newsplit.Released = false;
newsplit.ProjectID = null;
newsplit.TaskID = null;
if (location == null) {
newsplit.LocationID = lotstat.ToLocationID ?? transitline.ToLocationID;
} else {
receiptEntry.splits.SetValueExt<INTranSplit.locationID>(newsplit, location);
}
newsplit = receiptEntry.splits.Insert(newsplit);
newsplit.MaxTransferBaseQty = newsplitqty;
newsplit.BaseQty = newsplitqty;
newsplit.Qty = newsplit.BaseQty.Value;
receiptEntry.UpdateCostSubItemID(newsplit, item);
receiptEntry.SetCostAttributes(tran, newsplit, item, tran.OrigRefNbr);
newsplit.UnitCost = PXCurrencyAttribute.BaseRound(receiptEntry, newsplit.UnitCost);
receiptEntry.splits.Update(newsplit);
return newsplit;
}
}
}

how can i get property and value of a class by reflection

i started with this! and i would like to avoid this
private IQueryable<Customer> FilterResult(string search, List<Customer> dtResult, List<string> columnFilters)
{
IQueryable<Customer> results = dtResult.AsQueryable();
results = results.Where(p => (search == null || (p.Name != null && p.Name.ToLower().Contains(search.ToLower()) || p.City != null && p.City.ToLower().Contains(search.ToLower())
&& (columnFilters[0] == null || (p.Name != null && p.Name.ToLower().Contains(columnFilters[0].ToLower())))
&& (columnFilters[1] == null || (p.City != null && p.City.ToLower().Contains(columnFilters[1].ToLower())))
);
return results;
}
by using reflection maybe.... because imagine i have 100 properties it would be easy to mess up ...so i tried this way i would like to use reflection to loop on all properties instead of making references to each of them(properties)
public IQueryable<Aangifte> FilterColumn<T>()
{
try
{
List<Aangifte> v = AangifteGetData.GetData(StartDate, EndDate);
List<Aangifte> v2 = new List<Aangifte>();
Aangifte Foo = new Aangifte();
List<string> propertEntity = new List<string>();
var search = Request.Form.GetValues("search[value]").FirstOrDefault();
int count = -1;
var results = v.AsQueryable();
List<string> columnName = new List<string>();
foreach (var prop in Foo.GetType().GetProperties())
{
var t = "";
if (!prop.Name.Contains("FORMAT"))
{
TVALUE = prop.GetValue(Foo, null);
t= prop.Name;
propertEntity.Add(t);
count++;
}
if (count < propertEntity.Count())
{
var newt = t;
var Z = Request.Form.GetValues("columns[" + count + "][search][value]").FirstOrDefault();
results = results.Where
(
p => (search == null || (t != null && t.ToLower().Contains(search.ToLower())))
&& (Request.Form.GetValues("columns[" + count + "][search][value]").FirstOrDefault() == null || (t != null && t.ToLower().Contains(Request.Form.GetValues("columns[" + count + "][search][value]").FirstOrDefault().ToLower())))
);
}
}
return results;
}
catch (Exception EX)
{
throw EX;
}
}

Lambda expression to check if value is null or equals

I am developing an Asp.Net MVC Application and i am new to Linq and CodeFirst. In my controller this is the action that i have written:
public ActionResult Filter(int? PaperType , int? PaperGram , int? Brand)
{
var FilteredResult ;
if (PaperType.HasValue && PaperGram.HasValue && Brand.HasValue) {
FilteredResult = db.Stocks.where(m => m.PaperType == PaperType && m.PaperGram == PaperGram && m.Brand == Brand);
}
else if (PaperType.HasValue && PaperGram.HasValue) {
FilteredResult = db.Stocks.where(m => m.PaperType == PaperType && m.PaperGram == PaperGram);
}
else if (PaperType.HasValue && Brand.HasValue) {
FilteredResult = db.Stocks.where(m => m.PaperType == PaperType && m.Brand == Brand);
}
// and ifs continue to last
/*
.
.
.
.
*/
else {
FilteredResult = db.Stocks;
}
return View(FilteredResult);
}
But i know that this is not the best way to do in Linq and Codefirst. So, can you give a better solution to this problem?
You can do this:
FilteredResult = db.Stocks.where(m => (m.PaperType == PaperType || !PaperType.HasValue)
&& (m.PaperGram == PaperGram || !PaperGram.HasValue)
&& (m.Brand == Brand || !Brand.HasValue));
What you want to avoid is code duplication.
Create your original IQueriable and then add your where clauses when necessary
public ActionResult Filter(int? PaperType, int? PaperGram, int? Brand)
{
var FilteredResult FilteredResult = db.Stocks.AsQueryable();
if(PaperType.HasValue)
{
FilteredResult = FilteredResult.where(m => m.PaperType == PaperType);
if(PaperGram.HasValue)
FilteredResult = FilteredResult.where(m => m.PaperGram == PaperGram );
if ( Brand.HasValue)
FilteredResult = FilteredResult.where(m => m.Brand == Brand);
}
return View(FilteredResult);
}
You can just assign all elements to list and then filter every element in each if condition
IEnumerable<Stock> filteredResult = db.Stocks.AsQueryable();
if (PaperType.HasValue)
{
filteredResult = filteredResult.Where(m => m.PaperType == PaperType);
}
if (PaperGram.HasValue)
{
filteredResult = filteredResult.Where(m => m.PaperGram== PaperGram);
}
if (Brand.HasValue)
{
filteredResult= filteredResult.Where(m => m.Brand== Brand);
}
return View(FilteredResult.ToList());

Update table using linq

I want to update table but its not working
Here is the code:
public Boolean setSectionTickSign(decimal Trans_ID, decimal Job_ID, string SectioName)
{
string sectionames = "";
Transcription_Master Trans_Mastr = new Transcription_Master();
try
{
var Trans_Master = (from Trans_Mast in r2ge.Transcription_Master where Trans_Mast.Transcription_Id == Trans_ID && Trans_Mast.Entity_Id == Job_ID select new
{
Trans_Mast.Completed_Trans_Sections
}).Distinct().ToList();
var complt_trans = Trans_Master.AsEnumerable().Where(dr = > dr.Completed_Trans_Sections != null).ToList();
if (complt_trans.Count == 0)
{
if (sectionames == "")
{
Trans_Mastr.Completed_Trans_Sections = SectioName;
}
}
else
{
Trans_Mastr.Completed_Trans_Sections = "," + SectioName;
}
int sc = r2ge.SaveChanges();
}
}
It does not update database..what is wrong in it??
You should change this piece of code to such a thing:
var Trans_Master = (from Trans_Mast in r2ge.Transcription_Master
where Trans_Mast.Transcription_Id == Trans_ID
&& Trans_Mast.Entity_Id == Job_ID
select Trans_Mast).Distinct().ToList();
In this case Your variable Trans_Maser will be refference to object from collection, so changes will be done on object taken from EF context, and SaveChanges will give correct result.
Solved my own problem Transcription_Master Trans_Mastr = new Transcription_Master(); no need to create new object
public Boolean setSectionTickSign(decimal Trans_ID, decimal Job_ID, string SectioName)
{
string sectionames = "";
try
{
var empQuery = r2ge.Transcription_Master.Where(l => l.Transcription_Id == Trans_ID && l.Entity_Id == Job_ID).ToList();
foreach (Transcription_Master Trans_Mastrr in empQuery)
{
if (empQuery.Count == 0)
{
if (sectionames == "")
{
Trans_Mastrr.Completed_Trans_Sections = SectioName;
}
}
else
{
Trans_Mastrr.Completed_Trans_Sections = Trans_Mastrr.Completed_Trans_Sections + "," + SectioName;
}
}
int sc = r2ge.SaveChanges();
}

Dynamic Linq Build Where Clause many Parameters

I will explain my Problem
so Firstly, I use Predicates Linq for Build Where clause dynamically.
I have to build dynamically because I don't know how many parameters will come. Let me give an example. For the A column can be one parameters however, for the B column can be 2 parameters like either value 'Gas' or 'Oil' which select but that's big problem I can not combine for these 2 column correctly.
So as a result, this code work but It return 0 Items. But there are I know.
public List<CarEntity> GetSearchByKCriteria(int cityId, List<string> fuelType, List<string> gearType, List<string> budget,
List<string> caroser, List<string> enginePower)
{
Expression<Func<Car, bool>> query = null;
Expression<Func<Car, bool>> combine = null;
foreach (var bud in budget)
{
if (budget.Count >= 1)
{
if (bud == "1")
{
if (budget.Count > 1)
{
query = car => car.Budget >= 20000 && car.Budget <= 34999;
}
else
{
query = car => car.Budget >= 20000 && car.Budget <= 34999;
}
}
else if (bud == "2")
{
if (query != null)
{
combine = car => (car.Budget >= 35000 && car.Budget <= 49999);
query = query.Or(combine);
}
else
{
query = car => car.Budget >= 35000 && car.Budget <= 49999;
}
}
}
}
foreach (var caros in caroser)
{
if (caros != "-1" && !string.IsNullOrEmpty(caros))
{
if (query != null)
{
if (query.Expand().ToString().ToLower().Contains("karoser"))
{
combine = car => (car.Karoser == caros);
query = query.And(combine);
}
else
{
combine = car => car.Karoser == caros;
query = query.And(combine);
}
}
else
{
query = car => car.Karoser == caros;
}
}
}
foreach (var fuel in fuelType)
{
if (fuel != "-1" && !string.IsNullOrEmpty(fuel))
{
if (query != null)
{
if (query.Expand().ToString().ToLower().Contains("yakituru"))
{
combine = car => (car.YakitTuru==fuel);
query = query.Or(combine);
}
else
{
combine = car => car.YakitTuru == fuel;
query = query.And(combine);
}
}
else
{
query = car => car.YakitTuru == fuel;
}
}
}
foreach (var gear in gearType)
{
if (gear!="-1"&& !string.IsNullOrEmpty(gear))
{
if (query != null)
{
if (query.Expand().ToString().ToLower().Contains("sanzimantipi"))
{
combine = car => (car.SanzimanTipi == gear);
query = query.Or(combine);
}
else
{
combine = car => car.SanzimanTipi == gear;
query = query.And(combine);
}
}
else
{
query = car => car.SanzimanTipi == gear;
}
}
}
foreach (var engine in enginePower)
{
if (enginePower.Count >= 1)
{
if (engine == "1")
{
if (query != null)
{
if (query.Expand().ToString().ToLower().Contains("silindirhacmi"))
{
combine = car => (car.SilindirHacmi >= 0 && car.SilindirHacmi <= 1600);
query = query.Or(combine);
}
else
{
combine = car => (car.SilindirHacmi >= 0 && car.SilindirHacmi <= 1600);
query = query.And(combine);
}
}
else
{
query = car => car.SilindirHacmi >= 0 && car.SilindirHacmi <= 1600;
}
}
if (engine == "3")
{
if (query != null)
{
if (query.Expand().ToString().ToLower().Contains("silindirhacmi"))
{
combine = car => (car.SilindirHacmi >= 1601 && car.SilindirHacmi <= 1800);
query = query.Or(combine);
}
else
{
combine = car => (car.SilindirHacmi >= 1601 && car.SilindirHacmi <= 1800);
query = query.And(combine);
}
}
else
{
query = car => car.SilindirHacmi >= 1601 && car.SilindirHacmi <= 1800;
}
}
}
using (var context = DataContextFactory.CreateContext())
{
var result = (from fkCar in context.Car.Where(query)
join pkCarBrand in context.CarBrand on fkCar.Marka equals pkCarBrand.ID
where fkCar.IsActive == true
select new
{
entity = fkCar,
joinEntity = pkCarBrand
});
List<CarEntity> theCarList = new List<CarEntity>();
foreach (var item in result)
{
CarEntity theEntity = Mapper.Map(item.entity);
theEntity.CarBrand = Mapper.Map(item.joinEntity);
theCarList.Add(theEntity);
}
return theCarList;
}
}
So thanks for reply,
I faced a similar challenge a while back, where I wanted to have a list of allowed values for an attribute where, if matched, the associated instance would pass the filter. I came up with the following extension method:
static public Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
if (null == valueSelector)
{
throw new ArgumentNullException("valueSelector");
}
if (null == values) { throw new ArgumentNullException("values"); }
ParameterExpression p = valueSelector.Parameters.Single();
if (!values.Any())
{
return e => false;
}
var equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal));
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
This is based on the discussion and code posted at http://www.velocityreviews.com/forums/t645784-linq-where-clause.html

Categories