var patientList = uow.patientRepo.Get(p=>p.NutrititnistId == nutritionistId).select(y=>y.Id).toList();
var logList = uow.changeLogRepo.Get(c=>(c.EntityType.Equals("Program")) ||(patientList.any(s=>s==c.PatientId)));
Instead of retrieving that patientList, I want to combine that two linq statements into one statement
what can I do..?
Assuming you have an association between ChangeLog and Patient, you should be able to do this...
var logList = uow.changeLogRepo.Get(c=>(c.EntityType.Equals("Program")) ||
(c => c.Patients.any(p => p.NutrititnistId == nutritionistId)));
Related
This code is from my sql query
SELECT Branchname
FROM Branch
WHERE NOT EXISTS (SELECT Branchname FROM vBranching
WHERE Branch.Branchname = vBranching.Branchname AND UserName = 'xCuttiepies');
iwant to execute it into linq c# web api
Its look like this
var Branch = _dbcontext.Branches.Select(c => c.Branchname).ToList();
var vBranching = _dbcontext.VBranchings.Select(a => a.BranchName).ToList();
var ass = Branch.Where(!Branch.Exists(Branch == vBranching));
return Ok(vBranching);
In Branch == vBranching it says cannot convert from bool to System.Predicate<string>
I want to fetch the data that not exists on that different tables.
You souldn't be using ToList except right at the end, and your lambda syntax is off, and the equivalent of EXISTS is Any. You are also returning the wrong value.
var Branches =
_dbcontext.Branches
.Where(b => !_dbcontext.VBranchings.Any(vb =>
vb.BranchName == b.BranchName && vb.UserName == "xCuttiepies"))
.Select(c => c.Branchname)
.ToList();
return Ok(Branches);
When making a basic LINQ query you can later use lambda expressions to add a where clause like this: query.Where(c => (init.Contains(c.user)));.
My problem is that I need to add two where clauses on a query that uses a join in the basic LINQ query.
I'm trying to replace my old basic LINQ queries with added lambda expressions so that i prevent duplicated code.
This is my code;
var query = from c in db.Clgcom
join u in db.Dvusr
on c.Comaut equals u.Gitusr
// && (initialen.Contains(c.Tstusr) // <-- query.Where(c => (initialen.Contains(c.Tstusr)));
// This is what im trying to replace// ^^ This works because its in the same table
// || initialen.Contains(u.Clgusr)) // <-- What do i type when i want to include both these conditions?
&& (c.Modid.StartsWith("C")
|| c.Modid.StartsWith("M"))
select c;
if(filter != null){
query = query.Where(c => (initialen.Contains(c.Tstusr)
|| initialen.Contains(u.Clgusr)));
// This doesn't work
}
Is there a way to use a lambda expression that would achieve adding these two conditions in my where clause?
Or should i replace ALL basic LINQ queries with using lambda expressions?
Basically you need to also defer the select by selecting both c and u to begin with and later just selecting c.
var temp = from c in db.Clgcom
join u in db.Dvusr on c.Comaut equals u.Gitusr
where c.Modid.StartsWith("C") || c.Modid.StartsWith("M")
select new {c, u};
if(filter != null){
temp = temp.Where(x => initialen.Contains(x.c.Tstusr)
|| initialen.Contains(x.u.Clgusr));
var query = temp.Select(x => x.c);
If the relationship between Clgcom and Dvusr is many to one then you could do the following as Clgcom should have a Dvusr navigation property based on the foreign key relationship.
var query = from c in db.Clgcom
where (c.Modid.StartsWith("C") || c.Modid.StartsWith("M")) && c.Dvuser != null
select c;
if(filter != null){
query = query.Where(c => initialen.Contains(c.Tstusr)
|| initialen.Contains(c.Dvusr.Clgusr));
I have a page with five text boxes, each one representing a field in my database table and a search button:
If I were using SQL I could build my SQL statement depending on which fields have data in them.
However, I want to use LINQ, and I'm at a loss as to how to accomplish this. For instance, take a look at the query below:
var db = new BookDBDataContext();
var q =
from a in db.Books
where a.Title.Contains(txtBookTitle) &&
a.Author.Contains(txtAuthor) &&
a.Publisher.Contains(txtPublisher)
select a.ID;
The query above will return data where all the fields match data in the table. But, what if the user didn't enter an Author in the txtAuthor field? If I were building this as a query string, I could check each field for data and add it to the query string. Since this is LINQ, I can't dynamically change the search criteria, it seems.
Any advice would be greatly appreciated!
var db = new BookDBDataContext();
var q = (from a in db.Books
where a.Title.Contains(txtBookTitle));
if(!String.IsNullOrEmpty(txtAuthor))
{
q = q.Where(a => a.Author.Contains(txtAuthor));
}
if(!String.IsNullOrEmpty(txtAuthor))
{
q = q.Where(a => a.Publisher.Contains(txtPublisher));
}
var id = q.Select(a => a.ID);
from a in db.Books
where (string.isNullorWhiteSpace(search) || a.Title.Contains(search)) &&
(string.isNullorWhiteSpace(txtAuthor) || a.Author.Contains(txtAuthor) ) &&
(string.isNullorWhiteSpace(txtPublisher) || a.Publisher.Contains(txtPublisher))
select a.ID;
My statement is
Contents.
Select(x=> new
{
ContentUsers = x.ContentUsers.
Where(t=>t.UserId==2).
Select(t=>t.ContentId)
}).
Where(y=>y.ContentUsers.Any())
It gives me some Ids that i want to use it my another statements.
Contents.Where(x=>x.Id == 633,634,635)
How can i merge them?
What you're missing is the Contains function. It will probably be simpler to express this in a single query expression. If I translate your code to a single query expression, I looks a something like this:
var content =
from c in Contents
let contentUsers =
from x in Contents
select new
{
ContentUsers =
from t in x.ContentUsers
where t.UserId == 2
select t.ContentId
}
where contentUsers.Any(cu => cu.ContentUsers.Contains(c.Id))
select c;
However, it looks like all you want is to get just the Content records associated with a given UserID in the ContentUsers collection. This is a lot easier.
var content =
from c in Contents
where c.ContentUsers.Any(t => t.UserId == 2)
select c;
or if you prefer
var content = Contents.Where(c => c.ContentUsers.Any(t => t.UserId == 2));
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