I've got some IQueryables I would like to OR together. My understanding is chaining them together using Where results in ANDing them together (though my understanding may be faulty.)
Here's an example of a query I would like to OR together, in lieu of using all the chained Where clauses:
var query = query.Where(x => x.Value == 1)
.Where(x => x.Name == name);
Instead of that, I'm looking for something like:
var query = query.Where(x => x.Value == 1)
.Or(x => x.Name == name)
.Or(x => x.SomeOtherValue == something else)
.Or(x => x.AChildObject.Items.Where(item => item.SomeValue == yet something else));
...in my case there are probably 10 or items I'd need to chain together like above.
I've been looking over posts like this one and I suppose I could use a series of || statements to chain things together, but am not sure if that's the way to go. It could get very hard to read.
In researching this online I'm running into meaty articles on PredicateBuilders and the like. I'm not an expert on LINQ by any means and I was hoping for some guidance?
It looks like that you can just use OR operator.
var query = query.Where(x => x.Value == 1
|| x.Name == name)
|| x => x.SomeOtherValue == something else)
|| x => x.AChildObject.Items.Where(item => item.SomeValue == yet something else))
You can also use Union or Concat if the data being combined from different sources, see Linq union usage?
Related
Earlier I put a question on Stackoverflow about how to remove duplicate records in a list of objects, based on a particular property within each object.
I got the answer I was looking for (see below), a query which returns a distinct list of objects using MainHeadingID as the property to remove duplicates.
public IList<tblcours> GetAllCoursesByOrgID(int id)
{
return _UoW.tblcoursRepo.All.
Where(c => c.tblCourseCategoryLinks.Any(cl => cl.tblUnitCategory.tblUnit.ParentID == id))
.GroupBy(c => c.MainHeadingID)
.Select(g => g.FirstOrDefault())
.ToList();
}
However, now I need more help! Is there anyway of amending the query above so that, it only removes duplicate values when MainHeadingID is not equal to 180. I tried amending GroupBy line to
.GroupBy(c => c.MainHeadingID != 180)
However, this didn't work.
Any help would be much appreciated with this.
Thanks.
Following works for LINQ to SQL:
return _UoW.tblcoursRepo.All
.Where(c => c.tblCourseCategoryLinks.Any(cl => cl.tblUnitCategory.tblUnit.ParentID == id))
.GroupBy(c => c.MainHeadingID)
//.SelectMany(g => g.Key == 180 ? g : g.Take(1))
.SelectMany(g => g.Take(g.Key == 180 ? Int32.MaxValue : 1))
.ToList();
Comments: SelectMany in query above selects all items from group where MainHeadingID equals to 180, but it takes only one item form other groups (i.e. distinct result). Linq to SQL cannot translate commented out part, but thanks to #usr there is way around.
Linq to Entities cannot translate even simplified query. I think only option for you in this case is simple concating result of two queries:
Expression<Func<tblcours, bool>> predicate = x =>
x.tblCourseCategoryLinks.Any(cl => cl.tblUnitCategory.tblUnit.ParentID == id)
int headingId = 180;
return _UoW.tblcoursRepo.All
.Where(c => c.MainHeadingID != headingId)
.Where(predicate)
.GroupBy(c => c.MainHeadingID)
.Select(g => g.FirstOrDefault())
.Concat(_UoW.tblcoursRepo.All
.Where(c => c.MainHeadingID == headingId)
.Where(predicate))
.ToList();
lazyberezovsky's answer fails due to an EF bug (which is not surprising given the quality of EF's LINQ support). It can be made to work with a hack:
.SelectMany(g => g.Key == 180 ? g.Take(int.MaxValue) : g.Take(1))
or
.SelectMany(g => g.Take(g.Key == 180 ? int.MaxValue : 1))
Note that performance will not be particularly good due to the way this is translated to SQL.
from _tupleRows in this.TupleSet
from _member in _tupleRows.Members
where (_member.HasChildMembers && !_member.DrilledDown)
select new
{
_member1 = _member,
_member2 = (from _searched in this.TupleSet
from _compareMember in _searched.Members
where (_member.UniqueName == _compareMember.UniqueName &&
_member.LevelDepth == _compareMember.LevelDepth &&
_compareMember.DrilledDown)
select _compareMember).FirstOrDefault()
};
I need to convert this simple LINQ expression to an equivalent lambda expression
from _tupleRows in this.TupleSet
from _member in _tupleRows.Members
where (_member.HasChildMembers && !_member.DrilledDown)
select new
{
_member1 = _member,
_member2 = (from _searched in this.TupleSet
from _compareMember in _searched.Members
where (_member.UniqueName == _compareMember.UniqueName
&& _member.LevelDepth == _compareMember.LevelDepth
&& _compareMember.DrilledDown)
select _compareMember).FirstOrDefault()
};
becomes:
this.TupleSet
.SelectMany(tupleRows =>
tupleRows.Members
.Where(member => member.HasChildMembers && !member.DrilledDown)
.Select(member => new
{
_member1 = member,
_member2 = this.TupleSet
.SelectMany(searched =>
searched.Members
.Where(compareMember =>
member.UniqueName == compareMember.UniqueName
&& member.LevelDepth == compareMember.LevelDepth
&& compareMember.DrilledDown))
.FirstOrDefault(),
}));
They're really not hard to convert. Just start at the top and append as you go.
Something like:
this.TupleSet.SelectMany(ts => ts.Members)
.Where(m => m.HasChildMembers && !m.DrilledDown)
.Select(m => new
{
_member1 = m,
_member2 = this.TupleSet.SelectMany(ts => ts.Members)
.Where(other => m.UniqueName == other.UniqueName
&& m.LevelDepth == other.LevelDepth
&& other.DrilledDown
)
.FirstOrDefault()
});
No guarantees this is perfect or will even compile, but then I'm not trying to do your work for you, I'm trying to give you a demonstration of how you might do these conversions yourself.
Also, do yourself a favor and use simpler identifiers in lambdas when you can. They're very limited scope, and you're chaining calls to make one big statement, so often it will usually be very obvious in context what a variable means. I may not have the best example here, but the original variable names were way too verbose, IMO.
The nested query also seems a little strange. If there is a way to structure your data so you don't have to do that nested query I think you'll get better perf and simpler queries.
I have a set of unique DateTimes (without time portion) that the user can select from the user interface. This is not only a range like "LastWeek" or "LastMonth". The user can selcet every single day he wants.
What might be the best way to Linq-query in EntityFramework for matching results? I have a table Foo with an Attribute CreatedAt, which stores information with time portion. Of course I dont want to check the CreatedAt-Property on client side, SqlServer should do the job for me.
I think it should be someting like:
var query = _entities.Foo.Where(x => x.UserID == user.ID);
if (selectedDates.IsNullOrEmpty() == false)
query = query.Where(x => x.CreatedAt 'IsIn' selectedDates);
or:
foreach (var date in selectedDates)
query = query.Where(x => x.CreatedAt.Year == date.Year && x.Month == date.Month && x.Day == date.Month) //but of course here I have the problem that I have to use the 'Or'-Operator, not 'And'.
How can I accomplish this?
You can use EntityFunctions.TruncateTime() to perform a truncation of the date on the server. Then something along the lines of this should work.
query = query.Where(x => selectedDates.Contains(EntityFunctions.TruncateTime(x));
You can call the Contains extension method on selectedDates. Entity Framework 4.0 will understand this and will translate it to an IN operator in SQL:
if (selectedDates.IsNullOrEmpty() == false)
query = query.Where(x => selectedDates.Contains(x.CreatedAt));
Try query = query.Where(x => selectedDates.Contains(x.CreatedAt));
Linq and EF4.
I have this Linq query in query syntax I would like convert into query method.
Are you able to do it? I tried more tha 2 hours without success :-(
Thanks for your time
CmsContent myContentObj = (from cnt in context.CmsContents
from categoy in cnt.CmsCategories
where categoy.CategoryId == myCurrentCategoryId && cnt.ContentId == myCurrentContentId
select cnt).Single();
My original answer selected the wrong item. It's a bit more complicated than what I had (which Ani has posted). Here's what I believe is an equivalent query however and should perform better:
CmsContent myContentObj =
context.CmsContents
.Where(cnt => cnt.ContentId == myCurrentId
&& cnt.CmsCategories
.Any(categoy => categoy.CategoryId == myCurrentCategoryId))
.Single();
Here is a non-direct translation that I believe performs the same task in much less code:
var myContentObj = context.CmsContents.Single(
x => x.ContentId == myCurrentContentId &&
x.CmsCategories.Any(y => y.CategoryId == myCurrentCategoryId)
);
Here's how the C# compiler actually does it, with some help from .NET Reflector to verify:
var myContentObj = context
.CmsContents
.SelectMany(cnt => cnt.CmsCategories,
(cnt, categoy) => new { cnt, categoy })
.Where(a => a.categoy.CategoryId == myCurrentCategoryId
&& a.cnt.ContentId == myCurrentContentId)
.Select(a => a.cnt)
.Single();
Essentially, the 'nested' from clauses results in a SelectMany call with a transparent identifier (an anonymous-type instance holding the 'parent' cnt and the 'child' categoy). The Where filter is applied on the anonymous-type instance, and then we do another Select projection to get back the 'parent'. The Single call was always 'outside' the query expression of course, so it should be obvious how that fits in.
For more information, I suggest reading Jon Skeet's article How query expressions work.
Currently, I have the following LINQ queries. How can I merge the two queries into one. Basically, write a LINQ query to bring back the results I'd get from
IEnumerable<int> deltaList = people2010.Except(allPeople);
except in a single query.
var people2010 = Contacts.Where(x => x.Contractors
.Any(d => d.ContractorsStatusTrackings
.Any(date => date.StatusDate.Year >= 2010)))
.Select(x => x.ContactID);
var allPeople = Contacts.Where(x => x.Contractors
.Any(m => m.ContactID == x.ContactID))
.Select(x=> x.ContactID);
Thanks!
Why can you not just do Except as you are doing? Don't forget that your people2010 and allPeople variables are just queries - they're not the data. Why not just use them as they are?
If that's not acceptable for some reason, please give us more information - such as whether this is in LINQ to Object, LINQ to SQL etc, and what's wrong with just using Except.
It sounds like you're just looking for a more elegant way to write your query. I believe that this is a more elegant way to write your combined queries:
var deltaList =
from contact in Contacts
let contractors = contact.Contractors
where contractors.Any(ctor => ctor.ContractorStatusTrackings
.Any(date => date.StatusDate.Year >= 2010))
&& !contractors.Any(m => m.ContactID == contact.ContactID)
select contact.ContactID