I have two lists. I am trying to write a LINQ query.
My lists: Products, Dealers.
var productList=_context.ProductList
.Where(x=> x.Date=>new DateTime(2021,02,01) ).ToList();
I want to add the T-SQL constraint (below) to my LINQ, I tried with foreach but I can not handle it.
DealerCode Not IN(Select DealerCode from Dealers D With (nolock) Where D.ProductId=P.ProductId)
I don't want to do like !x.DealerCode.Contains(.....), because my lists are so big and I will use this constraint in 8 different queries. I am searching How can I do that in a more efficient way.
Edit after comments:
T-SQL Query
Select * From ProductList P
Where Date='2021-02-01'
AND DealerCode Not IN(Select DealerCode from Dealers D With (nolock) Where D.ProductId=P.ProductId)
I am trying to write the query in LINQ
you can use this query
var query = _context.ProductList
.Where(a => a.Date != (_context.ProductList
.Join(_context.Dealers,
right => right.ProductId,
left => left.ProductId,
(right, left) => new
{
right = right,
left = left
})
.Where(x => x.left.Date == a.Date)
.Select(y => y.left.Date)).FirstOrDefault());
Add the appropriate Navigation Properties to your models. Products should have a navigation property called "Dealers" and Dealers should have a navigation property called "Products". Once that is complete, your query becomes:
var query = _context.ProductList
.Where(p => p.Date = new DateTime(2021,02,01))
.Where(p => !p.Dealers.Any());
Assuming of course you are looking for products that have no dealers for it yet. Or something similar. Hard to tell without the complete models for ProductList, Dealers, etc.
Related
I want to create a linq to sql query that will return a list of objects with a sublist that has been filtered.
It sounds easy but I'm not sure how to make this to work
Here the SQL Query which returns what I want:
select * from Texts t inner join Translations tt on t.TranslationId = tt.Id
inner join Pages p on tt.Id = p.TranslationId and tt.NeutralText = p.TitleNeutralTextId
where t.LanguageId = 1
Now I have to write this with linq.
What I've done so far is:
var query = this.Queryable() // Page entity
.AsNoTracking()
.Include(x => x.TitleTranslation.Texts);
return (from m in query
from l in m.TitleTranslation.Texts
where m.TitleTranslation.Texts.Any(l => l.LanguageId == 1)
select m);
But it didn't work because I got the sublist with all languages instead of language with id #1 only.
Thanks for helping,
David
Any specific reason you are writing query? Either you can use Eager Loading of EF to load all the child tables, Or below Linq statement can fetch the required result
var result = texts.Join(translations, t => t.TranslationId, tt => tt.Id, (t, tt) => new {t, tt})
.Join(pages, ttt => new { Id = ttt.tt.Id, NeutralTextId = ttt.tt.NeutralText }, p => new { Id = p.TranslationId, NeutralTextId = p.TitleNeutralTextId }, (ttt, p) => new {ttt, p})
.Where(tttt => tttt.ttt.t.LanguageId == 1);
Here replace texts, translations and pages with actual dbContext entities collection property.
I think you must try lime this. this will work for you .
This will be similar to sql query
One way to do this .
var result = from m in Texts
join Translations on Texts.TranslationId = Translation.Id
Join Pages on Translations.NeutralText = Pages.NeutralText
where Texts.LanguageId = 1
select m
There an other way to do this using entity framework
var result =
this.Queryable().AsNoTracking().Include(x=>x.Translations).Where(x=>x.LanguageId= 1)
I found the solution I wanted thanks to Hasnain Bukhari.
The solution was to start from the text table, assign the filter, include the desired Entity (Page) and put the results into memory (ToList()). Then select pages. It will give the result I want in the order I have to.
var query = textService.Queryable()
.AsNoTracking()
.Include(x => x.Translation.Pages)
.Where(x => x.LanguageId == languageId).ToList();
return query.SelectMany(x => x.Translation.Pages);
I have the following nHibernate query that works well for me to pull an entity and eager fetch its sub collections.
var contactInfo = session.QueryOver<PeopleInfo>()
.Fetch(x => x.Addresses).Eager
.Fetch(x => x.EmailAddresses).Eager
.Fetch(x => x.PhoneNumbers).Eager
.Where(x => x.Id == contactInfoId)
.SingleOrDefault();
Now, let's say in Addresses that there is a field called Active,
is there a way to just return all the active addresses in one call?
I can filter it out after the fact, I'm just wondering if there's
a way to do it via query over.
Thanks!
Yes, you can do this, but you need to include some joins in your QueryOver query.
Under the hood, .Fetch is generating a bunch of LEFT JOINs to bring back the full list of PeopleInfo without excluding people that don't have any of the associated collections that you're fetching eagerly.
You can override the way the join is performed by performing a left join yourself.
For example, if you want to get all PeopleInfo, and only addresses that are Active, you could do the following:
Address addressAlias;
var addressRestriction = Restrictions.Where(() => address.Active);
session.QueryOver<PeopleInfo>()
.Left.JoinQueryOver(
pi => pi.Addresses, () => addressAlias, addressRestriction)
.Fetch(x => x.Addresses).Eager
.Fetch(x => x.EmailAddresses).Eager
.Fetch(x => x.PhoneNumbers).Eager
.Where(x => x.Id == contactInfoId)
.SingleOrDefault();
Now, since you're JOINing on Address and selecting out the entire PeopleInfo entity, you don't actually need to use .Fetch to pull back all of the Address fields.
session.QueryOver<PeopleInfo>()
.Left.JoinQueryOver(
x => x.Addresses, () => addressAlias, addressRestriction)
.Fetch(x => x.EmailAddresses).Eager
.Fetch(x => x.PhoneNumbers).Eager
.Where(x => x.Id == contactInfoId)
.SingleOrDefault();
The Address columns are included in the SELECT clause because you've joined on Address and are selecting out the entire PeopleInfo class.
These both generate the same SQL, but you should verify in a profiler. It should look something like this:
SELECT this_.* -- All PeopleInfo columns
addressali1_.*, -- All Address columns
emailaddre4_.* -- All email address columns
FROM PeopleInfo this_
LEFT OUTER JOIN address addressali1_
ON this_.id = addressali1_.personid
AND ( addressali1_.active = 1)
LEFT OUTER JOIN emailaddress emailaddre4_
ON this_.id = emailaddre4_.personid
WHERE this_.id = <your id>
I am struggling converting the following SQL query I wrote into Linq. I think I'm on the right track, but I must be missing something.
The error I'm getting right now is:
System.Linq.IQueryable does not contain a definition for .Contains
Which is confusing to me because it should right?
SQL
select Users.*
from Users
where UserID in (select distinct(UserID)
from UserPermission
where SupplierID in (select SupplierID
from UserPermission
where UserID = 6))
LINQ
var Users = (from u in _db.Users
where (from up in _db.UserPermissions select up.UserID)
.Distinct()
.Contains((from up2 in _db.UserPermissions
where up2.UserID == 6
select up2.SupplierID))
select u);
EDIT: I ended up going back to SqlCommand objects as this was something I had to get done today and couldn't waste too much time trying to figure out how to do it the right way with Linq and EF. I hate code hacks :(
I think there is no need to do a distinct here (maybe I am wrong). But here is a simpler version (assuming you have all the navigational properties defined correctly)
var lstUsers = DBContext.Users.Where(
x => x.UserPermissions.Any(
y => y.Suppliers.Any(z => z.UserID == 6)
)
).ToList();
Above if you have UserID field in Supplier entity, if it is NOT you can again use the navigational property as,
var lstUsers = DBContext.Users.Where(
x => x.UserPermissions.Any(
y => y.Suppliers.Any(z => z.User.UserID == 6)
)
).ToList();
Contains() only expects a single element, so it won't work as you have it written. Try this as an alternate:
var Users = _db.Users
.Where(u => _db.UserPermissions
.Select(x => UserID)
.Distinct()
.Where(x => _db.UserPermissions
.Where(y => y.UserID == 6)
.Select(y => y.SupplierID)
.Contains(x))
);
I didn't try on my side but you can try using the let keyword:
var Users = (from u in _db.Users
let distinctUsers = (from up in _db.UserPermissions select up).Distinct()
let subQuery = (from up2 in _db.UserPermissions
where up2.UserID == 6
select up2)
where
distinctUsers.SupplierID== subQuery.SupplierID &&
u.UserID==distinctUsers.UserID
select u);
So I have 2 IQueryable<Array> queries with a common string type; I want to use the second query to remove all records using the common type in the first, leaving only the records not found as the result of the first query. Not quite sure how to do it.
The most understandable way to do it is to filter out the items in ListA using Where():
IQueryable<LISTA> as = db.ListA.Where(x => x.Active);
IQueryable<LISTB> bs = db.ListB.Where(x => x.HadReview);
as = as.Where(a => bs.Any(b => a.LogginID == b.LogginID) == false);
A way that might be more efficient is to use a Group Join:
var as = from a in db.ListA.Where(x => x.Active)
join b in db.ListB.Where(x => x.HadReview)
on a.LogginID equals b.LogginID into bs
where bs.Any() == false
select a;
Edit: If you have two different DataContexts the above will not work. This might:
IQueryable<LISTA> as = db1.ListA.Where(x => x.Active);
IQueryable<LISTB> bs = db2.ListB.Where(x => x.HadReview);
as = as.Where(a => bs.Select(b => b.LogginID).Contains(a.LogginID) == false);
See also: Linq to SQL - How to inner join tables from different Data Context?
i have 4 table in SQL: DocumentType,ClearanceDocument,Request, RequestDocument.
i want when page load and user select one request, show all Document Based on clearanceType in RequestTable and check in RequestDocument and when exist set is_exist=true
I have written this query with SqlServer Query Editor for get result this Scenario but i can't convert this Query to Linq
select *,
is_Orginal=
(select is_orginal from CLEARANCE_REQUEST_DOCUMENT
where
DOCUMENT_ID=a.DOCUMENT_ID and REQUEST_ID=3)
from
DOCUMENT_TYPES a
where
DOCUMENT_ID in
(select DOCUMENT_ID from CLEARANCE_DOCUMENTS dt
where
dt.CLEARANCE_ID=
(SELECT R.CLEARANCE_TYPE FROM CLEARANCE_REQUEST R
WHERE
R.REQUEST_ID=3))
i write this Query in linq but not work
var list = (from r in context.CLEARANCE_REQUEST
where r.REQUEST_ID == 3
join cd in context.CLEARANCE_DOCUMENTS on r.CLEARANCE_TYPE equals cd.CLEARANCE_ID
join dt in context.DOCUMENT_TYPES on cd.DOCUMENT_ID equals dt.DOCUMENT_ID into outer
from t in outer.DefaultIfEmpty()
select new
{
r.REQUEST_ID,
cd.CLEARANCE_ID,
t.DOCUMENT_ID,
t.DOCUMENT_NAME,
is_set=(from b in context.CLEARANCE_REQUEST_DOCUMENT where
b.REQUEST_ID==r.REQUEST_ID && b.DOCUMENT_ID==t.DOCUMENT_ID
select new{b.IS_ORGINAL})
}
).ToList();
I want convert this Query to LINQ. Please help me. Thanks.
There is no need to manually join objects returned from an Entity Framework context.
See Why use LINQ Join on a simple one-many relationship?
If you use the framework as intended your job will be much easier.
var result = var clearanceTypes = context.CLEARANCE_REQUEST
.Single(r => r.REQUEST_ID == 3)
.CLEARANCE_DOCUMENTS
.SelectMany(dt => dt.DOCUMENT_TYPES)
.Select(a => new
{
DocumentType = a,
IsOriginal = a.CLEARANCE_REQUEST_DOCUMENT.is_original
});
Since your query won't be executed untill you iterate over the data, you can split your query in several subqueries to help you obtain the results like this:
var clearanceIds = context.CLEARANCE_REQUEST
.Where(r => r.REQUEST_ID == 3)
.Select(r => r.CLEARANCE_TYPE);
var documentIds = context.CLEARANCE_DOCUMENTS
.Where(dt => clearanceIds.Contains(dt.CLEARANCE_ID))
.Select(dt => dt.DOCUMENT_ID);
var result = context.DOCUMENT_TYPES
.Where(a => documentIds.Contains(a.DOCUMENT_ID))
.Select(a => new
{
// Populate properties here
IsOriginal = context.CLEARANCE_REQUEST_DOCUMENT
.Single(item => item.DOCUMENT_ID == a.DOCUMENT_ID &&
item.REQUEST_ID == 3)
.IS_ORIGINAL
})
.ToList();