C# Linq where clause - c#

I'm trying to create a linq query where clause is a constructed variable based on user choices
if (!string.IsNullOrEmpty(txbbox.Text))
{
query = "s.date== DateTime.Parse(txbbox.Text)";
}
if (!string.IsNullOrEmpty(txbbox.Text))
{
query = query + " & (s.type.Contains(txbbox.Text))";
}
there is a way to pass the variable built in the where clause in a LINQ query?
Stemp = (from s in SList
where ***query***
orderby s.DataScadenza
select s).ToList();

Yes; quite simple in this case, actually:
IEnumerable<Whatever> query = SList; // avoid var here, as will impact later assignment
if (!string.IsNullOrEmpty(txbbox.Text))
{
var when = DateTime.Parse(txbbox.Text); // to avoid parsing per item
query = query.Where(s => s.date == when);
}
if (!string.IsNullOrEmpty(txbbox.Text))
{
query = query.Where(s => s.type.Contains(txbbox.Text));
}
Stemp = query.OrderBy(s => s.DateScadenze).ToList();

Related

Linq LET and Dynamic Linq OrderBy

I'm using linq in nhibernate and I have problem when I try to use OrderBy using Dynamic Linq:
var bugAndFeatures = (from u in session.Query<BugAndFeature>()
let uEmployeeFullName = string.Format("{0} {1}", u.Employee.Surname, u.Employee.FirstName)
where u.IsDelete == false
select new BugAndFeatureView()
{
Id = u.Id,
Title = u.Title,
Description = u.Description,
EmployeeFullName = uEmployeeFullName,
EmployeeLogin = u.Employee.User.Login,
AssignedEmployeeId = u.AssignedEmployee == null ? 0 : u.AssignedEmployee.Id,
AssignedEmployeeFullName = string.Format("{0} {1}", u.AssignedEmployee.Surname, u.AssignedEmployee.FirstName),
CreateDate = u.CreateDate.Equals(null) ? string.Empty : u.CreateDate.ToString(),
LastUpdateDate = u.LastUpdateDate.Equals(null) ? string.Empty : u.LastUpdateDate.ToString()
}
);
After that I would like to use dynamic linq and use OrderBy like OrderBy("EmployeeFullName DESC") and I couldn't do that because I have exception.
I noticed that when I'm doing OrderBy on any property which I don't format or check null etc. I don't have this problem it only occurs when I try to do orderby with formated properties.
If I do ToList() before OrderBy then I don't have this problem but I don't want to do that I need use IQueryable.
How can I modify this query to solve my issue?
Thanks for help.
Have you try like this:
var Results = bugAndFeatures.ToList().OrderBy(p => p.EmployeeFullName);
IQueryable<Foo> query = ...;
switch (orderByParameter)
{
case "SomeValueParamter":
query = query.OrderBy(x => x.SomeValueParamter);
break;
case "SomeValueParamter":
query = query.OrderBy(x => x.SomeValueParamter);
break;
// etc
}

c# linq syntax slow due to multiple queries in single query

I am wondering if there is a better, more efficient way to re-code the linq syntax below to make the query run faster i.e. with a single call to the database. My database is located remotely which causes this to be quite slow:
var query = (from ticket in dataClassesDataContext.Tickets.Where(TicketsToShow.And(SearchVals))
select new
{
Priority = ticket.TicketPriority.TicketPriorityName,
Ticket = string.Format(TicketFormat, ticket.TicketID),
AssetId = ticket.Asset.Serial,
OpenDate = ticket.CheckedInDate,
OpenFor = CalculateOpenDaysAndHours(ticket.CheckedInDate, ticket.ClosedDate),
Account = ticket.Account.Customer.Name,
Description = ticket.Description.Replace("\n", ", "),
Status = ticket.TicketStatus.TicketStatusName,
Closed = ticket.ClosedDate,
THIS IS THE CAUSE ====>>> Amount = GetOutstandingBalanceForTicket(ticket.TicketID),
Paid = ticket.Paid,
Warranty = ticket.WarrantyRepair,
AssetLocation = GetAssetLocationNameFromID(ticket.Asset.LocationID, AssLocNames)
}).Skip(totalToDisplay * page).Take(totalToDisplay);
if (SortOrder.ToLower().Contains("Asc".ToLower()))
{
query = query.OrderBy(p => p.OpenDate);
}
else
{
query = query.OrderByDescending(p => p.OpenDate);
}//ENDIF
The main cause for the poor performance is the code in the function GetOutstandingBalanceForTicket below which calculates the sum of all items in an invoice and returns this as a total in a string:
public static string GetOutstandingBalanceForTicket(int TicketID)
{
string result = string.Empty;
decimal total = 0;
try
{
using (DataClassesDataContext dataClassesDataContext = new DataClassesDataContext(cDbConnection.GetConnectionString()))
{
var queryCustomerTickets = from ticket in dataClassesDataContext.Tickets
where
(ticket.TicketID == TicketID)
select ticket;
if (queryCustomerTickets != null)
{
foreach (var ticket in queryCustomerTickets)
{
var queryTicketChargeItems = from chargeItem in dataClassesDataContext.ProductChargeItems
where chargeItem.ChargeID == ticket.ChargeID &&
chargeItem.Deleted == null
select chargeItem;
foreach (var chargeItem in queryTicketChargeItems)
{
total += (chargeItem.Qty * chargeItem.Price);
}
}
}
}
}
catch (Exception ex)
{
}
return total.ToString("0.##");
}
Thank you in advance.
As you pointed out this code is quite slow as a query will be required for each ticket.
to eliminate the need for multiple queries you should look at applying an inner join between the ticketsToShow and the tickets entity (on the ticketid), using groupby to provide the sum of the charges for each ticket.
This is well illustrated in the answers to LINQ: Using INNER JOIN, Group and SUM
Ideally you would probably approach it more as an eager loading all at once type of setup. However, I do not think linq2sql supports that (I know EF does). One thing you can do is avoid the nested query though. Since you already have access to the ticket table, perhaps you should just issue a Sum() on it from your select statement. Hard for me to verify if any of this is an improvement so this code is kind of on the fly if you will.
//(from ticket in dataClassesDataContext.Tickets.Where(TicketsToShow.And(SearchVals))
(from ticket in dataClassesDataContext.Tickets
//this would be where you could eager load if possible (not entirely required)
//.Include is an EF method used only as example
/*.Include(t => t.TicketPriority)//eager load required entities
.Include(t => t.Asset)//eager load required entities
.Include(t => t.Account.Customer)//eager load required entities
.Include(t => t.TicketStatus)//eager load required entities
.Include(t => t.ProductChargeItems)//eager load required entities
*/
.Where(TicketsToShow.And(SearchVals))
select new
{
Priority = ticket.TicketPriority.TicketPriorityName,
Ticket = string.Format(TicketFormat, ticket.TicketID),
AssetId = ticket.Asset.Serial,
OpenDate = ticket.CheckedInDate,
OpenFor = CalculateOpenDaysAndHours(ticket.CheckedInDate, ticket.ClosedDate),
Account = ticket.Account.Customer.Name,
Description = ticket.Description.Replace("\n", ", "),
Status = ticket.TicketStatus.TicketStatusName,
Closed = ticket.ClosedDate,
//Use Sum and the foreign relation instead of a nested query
Amount = ticket.ProductChargeItems.Where(pci => pci.Deleted == null).Sum(pci => pci.Qty * pci.Price),
Paid = ticket.Paid,
Warranty = ticket.WarrantyRepair,
AssetLocation = GetAssetLocationNameFromID(ticket.Asset.LocationID, AssLocNames)
}).Skip(totalToDisplay * page).Take(totalToDisplay);
if (SortOrder.ToLower().Contains("Asc".ToLower()))
{
query = query.OrderBy(p => p.OpenDate);
}
else
{
query = query.OrderByDescending(p => p.OpenDate);
}
I think, you can make this query simplier. Somethink like this:
public static string GetOutstandingBalanceForTicket(DataClassesDataContext context, int TicketID)
{
decimal total = 0;
var total = (from ticket in context.Tickets
join chargeItem from context.ProductChargeItems on chargeItem.ChargeID == ticket.ChargeID
where (ticket.TicketID == TicketID && chargeItem.Deleted == null)
select chargeItem).Sum(chargeItem => chargeItem.Qty * chargeItem.Price);
return total.ToString("0.##");
}
/*...*/
Amount = GetOutstandingBalanceForTicket(dataClassesDataContext, ticket.TicketID),
Now, you can inline this methos in your query.
It can contains syntax errors, because I wrote it in notepad.

Dynamic where clause in LINQ?

I am trying to load data based on Dynamic where condition.
string tempQry = string.Empty;
if (!string.IsNullOrEmpty(cusid) && !string.IsNullOrEmpty(mktid))
tempQry = "x=>x.MarketID==" + mktid + "&& x.MasterCustomerID==" + cusid;
if (string.IsNullOrEmpty(cusid))
tempQry = "x=>x.MarketID==" + mktid;
if (string.IsNullOrEmpty(mktid))
tempQry = "x=>x.MasterCustomerID==" + cusid;
_lstOptInInterest = new LinkedList<OptInInterestArea>(
(from a in _lstOptInInterest
join b in _marketoEntities.CustCommPreferences.Where(tempQry)
on new { CODE = a.Code, SUBCODE = a.SubCode } equals new { CODE = b.Option_Short_Name, SUBCODE = b.Option_Short_Subname }
into leftGroup
from b in leftGroup.DefaultIfEmpty()
select new OptInInterestArea()
{
Code = a.Code,
SubCode = a.SubCode,
SubCodeDescription = a.SubCodeDescription,
CodeDescription = a.CodeDescription,
PrevOptIn = b != null && b.OptedIn == true
}).ToList());
It is giving compilation error Where(tempQry).
'System.Data.Entity.DbSet<Market.Data.CustCommPreference>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,bool>>)' has some invalid arguments
How to handle this?
Where awaits conditions in form of lambdas rather than strings, so you have to refactor your code a little bit (just an idea below):
IQueryable<CustCommPreference> query = _marketoEntities.CustCommPreferences.AsQueryable();
if (!string.IsNullOrEmpty(cusid))
query = query.Where(x => x.MasterCustomerID == cusid);
if (!string.IsNullOrEmpty(mktid))
query = query.Where(x => x.MarketID == mktid);
and later use it:
...
join b in query
...
see this blog by Scott. It should help you sort your issue
The error you are seeing appears to indicate that you are using EF not LINQ to SQL. Please correct your tags if that is the case. If you want to use strings, consider using ObjectQuery's Where method instead of using DBSet. Alternatively, you could build the entire query using EntitySQL.

filter a linq query based on the results of another query's results

I am wanting to filter a linq query
I have 2 linq statements
The 1st gets all the stores I want and the 2nd is where I filter information based on the results found in the 1st query.
var stores = ctx.Stores.Where(ps => ps.ParentStoreID == parent.ParentStoreID && ps.StoreID!=storeID);
var query = (from a in ctx.TransactionTable
from b in ctx.MappingTable.Where(x => x.TransactionId== a.TransactionId).DefaultIfEmpty()
where a.StoreID!=storeID
select new
{
Transactions = a,
Mapping = b
}).ToList();
How do I add another where clause into my 2nd query to only return results where a.StoreId is contained within the stores result?
Like this:
var stores = ctx.Stores.Where(ps => ps.ParentStoreID == parent.ParentStoreID && ps.StoreID!=storeID);
var query = (from a in ctx.TransactionTable
from b in ctx.MappingTable.Where(x => x.TransactionId==a.TransactionId).DefaultIfEmpty()
where a.StoreID!=storeID && stores.Select(s => s.StoreID).Contains(a.StoreID)
select new
{
Transactions = a,
Mapping = b
}).ToList();
You can find more info here:
Linq to Entities - SQL "IN" clause

LINQ Queries And Context

I have a slight issue with some code I'm writing
if(parameter == 1)
{
var linq = from a in db.table select a;
}
else
{
var linq = from a in db.table where a.id = 1 select a;
}
foreach(var b in linq)
{
...
}
So basically what's going on is that the variable "linq" is different depending on the value of "parameter". When I try to loop through "linq" with my foreach loop, I get an error about how linq doesn't exist in the current context.
What is the best way to work around this type of issue?
What you tried doesn't work because the variable linq is already out of scope when you try to use it. You need to move the declaration to the outer scope.
To answer your question in a general way first: if you need to declare a variable before you assign to it, you can't use var. You need to declare the type explicitly:
IQueryable<Something> linq;
if(parameter == 1)
{
linq = from a in db.table select a;
}
else
{
linq = from a in db.table where a.id == 1 select a;
}
In your particular example though you can simplify things:
var query = from a in db.table select a;
if (parameter != 1)
{
query = query.Where(a => a.id == 1);
}
I dunno if this is the best way but, assuming you're returning the same table.
var linq = from a in db.table where a.id = 1 select a;
if(parameter == 1)
{
linq = from a in db.table select a;
}
//foreach.
You can reconstruct your linq query and not pay a big penalty, since you haven't actually executed it.

Categories

Resources