This might be bit tricky here for me, as I am new to LINQ.
I have following LINQ code returns me data properly.
But, the conditions given are selective.
Means, all the conditions may or may not be given at a time.Conditions are user input based. So, how to write such incremental LINQ based on criteria selected by user.
var dRows = (from TblPatientInformation in this.dsPrimaPlus1.tblPatientInformation
join TblDoctorMaster in this.dsPrimaPlus1.tblDoctorMaster on new { PtI_dcMId = Convert.ToInt32(TblPatientInformation.ptI_dcMId) } equals new { PtI_dcMId = TblDoctorMaster.dcM_Id }
join TblDepartmentMaster in this.dsPrimaPlus1.tblDepartmentMaster on new { DcM_deptMId = TblDoctorMaster.dcM_deptMId } equals new { DcM_deptMId = TblDepartmentMaster.ID }
join TblPatientDiagnosis in this.dsPrimaPlus1.tblPatientDiagnosis on new { PtI_Id = TblPatientInformation.ptI_Id } equals new { PtI_Id = Convert.ToInt32(TblPatientDiagnosis.ptD_ptIId) }
join TblDiagnosisInformation in this.dsPrimaPlus1.tblDiagnosisInformation on new { PtD_tgIId = Convert.ToInt32(TblPatientDiagnosis.ptD_tgIId) } equals new { PtD_tgIId = TblDiagnosisInformation.tgI_Id }
where
TblPatientInformation.ptI_Id > 0 ||
TblPatientInformation.ptI_PatientName.Contains(txtName.Text) ||
TblPatientInformation.ptI_Code == int.Parse( txtCaseNo.Text) ||
TblDepartmentMaster.ID ==int.Parse( cmbDepartment.SelectedValue.ToString()) ||
TblDoctorMaster.dcM_Id == int.Parse(cmbDoctor.SelectedValue.ToString()) ||
TblDiagnosisInformation.tgI_Id == int.Parse(cmbDiagnosis.SelectedValue.ToString())
select new
{
TblPatientInformation.ptI_Id,
TblPatientInformation.ptI_Code,
TblPatientInformation.ptI_PatientName,
TblPatientInformation.ptI_dcMId,
TblPatientInformation.ptI_Age,
TblPatientInformation.ptI_Address,
TblPatientInformation.ptI_eMail,
TblPatientInformation.ptI_Phone1,
TblPatientInformation.ptI_Phone2,
TblPatientInformation.ptI_Phone3,
TblPatientInformation.ptI_Date,
TblPatientInformation.ptI_Gender,
TblDiagnosisInformation.tgI_Name,
TblDiagnosisInformation.tgI_Description,
TblDoctorMaster.dcM_FullName,
TblDepartmentMaster.Department
});
I recommend trying out Predicate Builder http://www.albahari.com/nutshell/predicatebuilder.aspx for this purpose.
That post recommends the following:
The easiest way to experiment with PredicateBuilder is with LINQPad.
LINQPad lets you instantly test LINQ queries against a database or
local collection and has direct support for PredicateBuilder (press F4
and check 'Include PredicateBuilder').
which is an easy way to get going with this approach.
Hope that helps.
One solution would be to use Dynamic LINQ, where you can specify string expressions, instead of code expressions. e.g.
// Dynamic Linq string expression
var result = context.People.Where("Age >= 3 And StreetNumber < 3").ToList();
as opposed to:
// Linq code expression
var result = context.People.Where(q=>q.Age>=3 && q.StreetNumber < 3).ToList();
With this, you can parse your expression based on the user input, e.g.
StringBuilder sb = new StringBuilder();
...
if(criteria1)
{
sb.Append(" And Criteria>1");
}
...
if(criteria2)
{
sb.Append(" And Criteria2<15");
}
...
var result = context.People.Where(sb.ToString()).ToList();
Check out this Scott Gu article for a complete example:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Related
Hi this is my first question so apologies if it is really basic - I am very new to programming!!!
Using c# in MVC I am trying to select object which has a Date property from entitymodel context. This date then selects the relevant Weight object and so on to get my list of "Set" objects.
The code works and does what I want but would like some general guidance on how to make this code more concise. Here is the code:
public ActionResult showDiary(string datein)
{
LocalTestEntities1 dblists = new LocalTestEntities1();
DateTime date = Convert.ToDateTime(datein);
IEnumerable<ExerciseDiary> diary = from o in dblists.ExerciseDiaries where o.Date == date select o;
var mydiary = diary.ToList();
ExerciseDiary thediary = mydiary[0];
IQueryable<Weight> weights = from o in dblists.Weights where o.DiaryID == thediary.ID select o;
var selectedWeight = weights.ToList();
Weight weight = selectedWeight[0];
IEnumerable<Set> sets = from x in dblists.Sets where x.WeightId == weight.WeightID select x;
return View(sets);
}
It seems that I am taking too many steps here. I know that I am only returning one object to diary. Is there a way to get this object from dblists without sending to an IEnumerable?
Using the First() method will make things a little more concise:
public ActionResult showDiary(string datein)
{
using (LocalTestEntities1 dblists = new LocalTestEntities1())
{
DateTime date = Convert.ToDateTime(datein);
var thediary = (from o in dblists.ExerciseDiaries
where o.Date == date
select o).First();
var weight = (from o in dblists.Weights
where o.DiaryID == thediary.ID
select o).First();
var sets = (from x in dblists.Sets
where x.WeightId == weight.WeightID
select x).ToList();
}
return View(sets);
}
You should also wrap your LINQ to Entities data access in a using block so it's properly disposed of.
There's always many ways to do things, but... I think the easiest way would be to use First() since you are always just grabbing the first result in a list.
Another way to make it a little cleaner is to put your LINQ statements on multiple lines like I did for sets.
You can also use var, which some people like and others don't to have the compiler infer the type. I did this with sets below. I feel it cleans up the code a bit when you have large declarations with IEnumerable and generics.
public ActionResult showDiary(string datein)
{
LocalTestEntities1 dblists = new LocalTestEntities1();
DateTime date = Convert.ToDateTime(datein);
ExerciseDiary thediary = dblists.ExerciseDiaries.First(o => o.Date == date);
Weight weight = dblists.Weights.First(o.DiaryID == thediary.ID);
var sets = from x in dblists.Sets
where x.WeightId == weight.WeightID
select x;
return View(sets);
}
IMO this is easier to read than what you had in your answer above.
Be careful using First() because it will throw an exception if there are no records.
public ActionResult showDiary(string datein)
{
using( var dblists = new LocalTestEntities1())
{
var date = Convert.ToDateTime(datein);
var thediary = dblists.ExerciseDiaries.First(o => o.Date == date);
var weight = dblists.Weights.First(o => o.DiaryID ==thediary.ID);
var sets = dblists.Sets.Where(x => x.WeightId == weight.WeightID).AsEnumerable();
return View(sets);
}
}
Warning: If it's possible the data wont always be there. Use FirstOrDefault instead and check for null values.
I have a LINQ expression that joins two tables. I want to conditionally check another boolean value: (notice text between ********* below)
bool status = testfunc();
var List =
from t in Houses
join k in Tbl_HouseOwner on t.HouseCode equals k.HouseCode
where k.ReqCode== t.ReqCode
*********** if (status) { where k.Name.Contains(Name) } **********
select new
{
...
name = k.Name,
...
};
You can use status to mask the condition, like this:
where k.ReqCode == t.ReqCode && (!status || k.Name.Contains(Name))
If the status is false, the OR || will succeed immediately, and the AND && will be true (assuming that we've got to evaluating the OR ||, the left-hand side of the AND && must have been true). If the status is true, on the other hand, the k.Name.Contains(Name) would need to be evaluated in order to finish evaluating the condition.
An alternative option to dasblinkenlight's answer (which should work fine) is to build up the query programmatically. In this case you're effectively changing the right hand side of the join, so you could use:
IQueryable<Owner> owners = Tbl_HouseOwner;
if (status)
{
owners = owners.Where(k => k.Name.Contains(Name));
}
Then:
var query = from t in Houses
join k in owners on t.HouseCode equals k.HouseCode
where k.ReqCode == t.ReqCode
select new { ... };
Which approach is the most suitable depends on your scenario. If you want to add a variety of different query filters, building it up programmatically can be cleaner - and make the final SQL easier to understand for any given query. For a one-off, dasblinkenlight's approach is simpler.
Also note that in LINQ to Objects at least, it would be more efficient to join on both columns:
var query = from t in Houses
join k in owners
on new { t.HouseCode, t.ReqCode } equals new { k.HouseCode, k.ReqCode }
select new { ... };
In any flavour of LINQ which translates to SQL, I'd expect this to be optimized by the database or query translation anyway though.
I do it this way:
IQueryable<X> r = from x in Xs
where (x.Status == "Active")
select x;
if(withFlagA) {
r = r.Where(o => o.FlagA == true);
}
To fit this to your example, firstly you could do this:
IQueryable<YourOwnerType> filteredOwners = Tbl_HouseOwner;
if( status ) {
filteredOwners = filteredOwners.Where( o => o.Name.Contains(Name) );
}
Then substitute Tbl_HouseOwner with filteredOwners.
var List =
from t in Houses
join k in filteredOwners on t.HouseCode equals k.HouseCode
where k.ReqCode== t.ReqCode
//Nothing here
select new
{
...
name = k.Name,
...
};
Now, you may know this, but the point here is that the initial .Where does not 'reach out' to the database. Your query doesn't get executed either until you start enumerating it (e.g. foreach) or call a method like ToList(), First(), FirstOrDefault(). This means you can call .Wheres after your selects if you prefer and the final query will still be efficient.
I'm using LINQ to Entities and I want to know how do I translate the following query to lambda expression using extension methods.
public _Deposito RegresaDepositosBancarios(int id)
{
return (from d in context.depositos_bancarios
where d.IDDeposito == id
select new _Deposito
{
idDeposito = d.IDDeposito,
cantidad = d.Monto,
fecha = d.FechaDeposito,
aplicado = d.Aplicado
}).Single();
}
Notice that I'm returning a _Deposito type, how do I achieve this using extension methods?
I need something like the following:
public Persona RegresaPersonaPorNombres(string nombres, string apellidoP, string apellidoM)
{
var p = context.personas.Where(x => x.Nombres == nombres &&
x.ApellidoP == apellidoP &&
x.ApellidoM == apellidoM).FirstOrDefault();
return p;
}
I don't want to return an entity type but a custom type instead
This is how this would be written with extension methods, but you really should not need to worry as they are both the same thing.
return context.depositos_bancarios
.Where(d=>d.IDDeposito == id)
.Select(d=>new _Deposito
{
idDeposito = d.IDDeposito,
cantidad = d.Monto,
fecha = d.FechaDeposito,
aplicado = d.Aplicado
})
.Single();
An interesting side note: I could have used a d=> in the Where and then an e=> in the Select. Whereas, the d propogates down throughout the phrase. The only way to reset it would be to use a let phrase. This has nothing to do with the direct question, but I just thought it interesting and wanted to point it out :)
(An earlier question, Recursively (?) compose LINQ predicates into a single predicate, is similar to this but I actually asked the wrong question... the solution there satisfied the question as posed, but isn't actually what I need. They are different, though. Honest.)
Given the following search text:
"keyword1 keyword2 ... keywordN"
I want to end up with the following SQL:
SELECT [columns] FROM Customer
WHERE (
Customer.Forenames LIKE '%keyword1%'
OR
Customer.Forenames LIKE '%keyword2%'
OR
...
OR
Customer.Forenames LIKE '%keywordN%'
) AND (
Customer.Surname LIKE '%keyword1%'
OR
Customer.Surname LIKE '%keyword2%'
OR
....
OR
Customer.Surname LIKE '%keywordN%'
)
Effectively, we're splitting the search text on spaces, trimming each token, constructing a multi-part OR clause based on each , and then AND'ing the clauses together.
I'm doing this in Linq-to-SQL, and I have no idea how to dynamically compose a predicate based on an arbitrarily-long list of subpredicates. For a known number of clauses, it's easy to compose the predicates manually:
dataContext.Customers.Where(
(
Customer.Forenames.Contains("keyword1")
||
Customer.Forenames.Contains("keyword2")
) && (
Customer.Surname.Contains("keyword1")
||
Customer.Surname.Contains("keyword2")
)
);
In short, I need a technique that, given two predicates, will return a single predicate composing the two source predicates with a supplied operator, but restricted to the operators explicitly supported by Linq-to-SQL. Any ideas?
You can use the PredicateBuilder class
IQueryable<Customer> SearchCustomers (params string[] keywords)
{
var predicate = PredicateBuilder.False<Customer>();
foreach (string keyword in keywords)
{
// Note that you *must* declare a variable inside the loop
// otherwise all your lambdas end up referencing whatever
// the value of "keyword" is when they're finally executed.
string temp = keyword;
predicate = predicate.Or (p => p.Forenames.Contains (temp));
}
return dataContext.Customers.Where (predicate);
}
(that's actually the example from the PredicateBuilder page, I just adapted it to your case...)
EDIT:
Actually I misread your question, and my example above only covers a part of the solution... The following method should do what you want :
IQueryable<Customer> SearchCustomers (string[] forenameKeyWords, string[] surnameKeywords)
{
var predicate = PredicateBuilder.True<Customer>();
var forenamePredicate = PredicateBuilder.False<Customer>();
foreach (string keyword in forenameKeyWords)
{
string temp = keyword;
forenamePredicate = forenamePredicate.Or (p => p.Forenames.Contains (temp));
}
predicate = PredicateBuilder.And(forenamePredicate);
var surnamePredicate = PredicateBuilder.False<Customer>();
foreach (string keyword in surnameKeyWords)
{
string temp = keyword;
surnamePredicate = surnamePredicate.Or (p => p.Surnames.Contains (temp));
}
predicate = PredicateBuilder.And(surnamePredicate);
return dataContext.Customers.Where(predicate);
}
You can use it like that:
var query = SearchCustomers(
new[] { "keyword1", "keyword2" },
new[] { "keyword3", "keyword4" });
foreach (var Customer in query)
{
...
}
Normally you would chain invocations of .Where(...). E.g.:
var a = dataContext.Customers;
if (kwd1 != null)
a = a.Where(t => t.Customer.Forenames.Contains(kwd1));
if (kwd2 != null)
a = a.Where(t => t.Customer.Forenames.Contains(kwd2));
// ...
return a;
LINQ-to-SQL would weld it all back together into a single WHERE clause.
This doesn't work with OR, however. You could use unions and intersections, but I'm not sure whether LINQ-to-SQL (or SQL Server) is clever enough to fold it back to a single WHERE clause. OTOH, it won't matter if performance doesn't suffer. Anyway, it would look something like this:
<The type of dataContext.Customers> ff = null, ss = null;
foreach (k in keywords) {
if (keywords != null) {
var f = dataContext.Customers.Where(t => t.Customer.Forenames.Contains(k));
ff = ff == null ? f : ff.Union(f);
var s = dataContext.Customers.Where(t => t.Customer.Surname.Contains(k));
ss = ss == null ? s : ss.Union(s);
}
}
return ff.Intersect(ss);
We're working on a Log Viewer. The use will have the option to filter by user, severity, etc. In the Sql days I'd add to the query string, but I want to do it with Linq. How can I conditionally add where-clauses?
if you want to only filter if certain criteria is passed, do something like this
var logs = from log in context.Logs
select log;
if (filterBySeverity)
logs = logs.Where(p => p.Severity == severity);
if (filterByUser)
logs = logs.Where(p => p.User == user);
Doing so this way will allow your Expression tree to be exactly what you want. That way the SQL created will be exactly what you need and nothing less.
If you need to filter base on a List / Array use the following:
public List<Data> GetData(List<string> Numbers, List<string> Letters)
{
if (Numbers == null)
Numbers = new List<string>();
if (Letters == null)
Letters = new List<string>();
var q = from d in database.table
where (Numbers.Count == 0 || Numbers.Contains(d.Number))
where (Letters.Count == 0 || Letters.Contains(d.Letter))
select new Data
{
Number = d.Number,
Letter = d.Letter,
};
return q.ToList();
}
I ended using an answer similar to Daren's, but with an IQueryable interface:
IQueryable<Log> matches = m_Locator.Logs;
// Users filter
if (usersFilter)
matches = matches.Where(l => l.UserName == comboBoxUsers.Text);
// Severity filter
if (severityFilter)
matches = matches.Where(l => l.Severity == comboBoxSeverity.Text);
Logs = (from log in matches
orderby log.EventTime descending
select log).ToList();
That builds up the query before hitting the database. The command won't run until .ToList() at the end.
I solved this with an extension method to allow LINQ to be conditionally enabled in the middle of a fluent expression. This removes the need to break up the expression with if statements.
.If() extension method:
public static IQueryable<TSource> If<TSource>(
this IQueryable<TSource> source,
bool condition,
Func<IQueryable<TSource>, IQueryable<TSource>> branch)
{
return condition ? branch(source) : source;
}
This allows you to do this:
return context.Logs
.If(filterBySeverity, q => q.Where(p => p.Severity == severity))
.If(filterByUser, q => q.Where(p => p.User == user))
.ToList();
Here's also an IEnumerable<T> version which will handle most other LINQ expressions:
public static IEnumerable<TSource> If<TSource>(
this IEnumerable<TSource> source,
bool condition,
Func<IEnumerable<TSource>, IEnumerable<TSource>> branch)
{
return condition ? branch(source) : source;
}
When it comes to conditional linq, I am very fond of the filters and pipes pattern.
http://blog.wekeroad.com/mvc-storefront/mvcstore-part-3/
Basically you create an extension method for each filter case that takes in the IQueryable and a parameter.
public static IQueryable<Type> HasID(this IQueryable<Type> query, long? id)
{
return id.HasValue ? query.Where(o => i.ID.Equals(id.Value)) : query;
}
Doing this:
bool lastNameSearch = true/false; // depending if they want to search by last name,
having this in the where statement:
where (lastNameSearch && name.LastNameSearch == "smith")
means that when the final query is created, if lastNameSearch is false the query will completely omit any SQL for the last name search.
Another option would be to use something like the PredicateBuilder discussed here.
It allows you to write code like the following:
var newKids = Product.ContainsInDescription ("BlackBerry", "iPhone");
var classics = Product.ContainsInDescription ("Nokia", "Ericsson")
.And (Product.IsSelling());
var query = from p in Data.Products.Where (newKids.Or (classics))
select p;
Note that I've only got this to work with Linq 2 SQL. EntityFramework does not implement Expression.Invoke, which is required for this method to work. I have a question regarding this issue here.
It isn't the prettiest thing but you can use a lambda expression and pass your conditions optionally. In TSQL I do a lot of the following to make parameters optional:
WHERE Field = #FieldVar OR #FieldVar IS NULL
You could duplicate the same style with a the following lambda (an example of checking authentication):
MyDataContext db = new MyDataContext();
void RunQuery(string param1, string param2, int? param3){
Func checkUser = user =>
((param1.Length > 0)? user.Param1 == param1 : 1 == 1) &&
((param2.Length > 0)? user.Param2 == param2 : 1 == 1) &&
((param3 != null)? user.Param3 == param3 : 1 == 1);
User foundUser = db.Users.SingleOrDefault(checkUser);
}
I had a similar requirement recently and eventually found this in he MSDN.
CSharp Samples for Visual Studio 2008
The classes included in the DynamicQuery sample of the download allow you to create dynamic queries at runtime in the following format:
var query =
db.Customers.
Where("City = #0 and Orders.Count >= #1", "London", 10).
OrderBy("CompanyName").
Select("new(CompanyName as Name, Phone)");
Using this you can build a query string dynamically at runtime and pass it into the Where() method:
string dynamicQueryString = "City = \"London\" and Order.Count >= 10";
var q = from c in db.Customers.Where(queryString, null)
orderby c.CompanyName
select c;
You can create and use this extension method
public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool isToExecute, Expression<Func<TSource, bool>> predicate)
{
return isToExecute ? source.Where(predicate) : source;
}
Just use C#'s && operator:
var items = dc.Users.Where(l => l.Date == DateTime.Today && l.Severity == "Critical")
Edit: Ah, need to read more carefully. You wanted to know how to conditionally add additional clauses. In that case, I have no idea. :) What I'd probably do is just prepare several queries, and execute the right one, depending on what I ended up needing.
You could use an external method:
var results =
from rec in GetSomeRecs()
where ConditionalCheck(rec)
select rec;
...
bool ConditionalCheck( typeofRec input ) {
...
}
This would work, but can't be broken down into expression trees, which means Linq to SQL would run the check code against every record.
Alternatively:
var results =
from rec in GetSomeRecs()
where
(!filterBySeverity || rec.Severity == severity) &&
(!filterByUser|| rec.User == user)
select rec;
That might work in expression trees, meaning Linq to SQL would be optimised.
Well, what I thought was you could put the filter conditions into a generic list of Predicates:
var list = new List<string> { "me", "you", "meyou", "mow" };
var predicates = new List<Predicate<string>>();
predicates.Add(i => i.Contains("me"));
predicates.Add(i => i.EndsWith("w"));
var results = new List<string>();
foreach (var p in predicates)
results.AddRange(from i in list where p.Invoke(i) select i);
That results in a list containing "me", "meyou", and "mow".
You could optimize that by doing the foreach with the predicates in a totally different function that ORs all the predicates.