How to add a second condition in Linq Query [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I have this linq query in my controller and it is working fine,
var Engineers = dbcontext.Employees.Where(i => i.Department == "Engineers");
but I would like to add a second condition to the query to filter out the results,
var Engineers = dbcontext.Employees.Where(i => i.Department == "Engineers" && i.UserStatus = 1);
Thanks for your help.

You made a typo, just need to use comparison operator (==) instead of assignation (=)
So it would be like that:
var Engineers = dbcontext.Employees.Where(i => i.Department == "Engineers" && i.UserStatus == 1);

Related

Unable to check if integer is equal to zero [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I just need it to check if the variable "remaining" equals 0
To check equality in C# use == operator.
if(remaining == 0){
// write your logic
}
Use the == operator
if(remaining == 0){
// write your logic
}
The == operator checks whether the LHS = RHS or not whereas
= is an assignment operator it is used to assign values like int a = 10;

Insert Multiple Where in Linq [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
SELECT AccessName
from Accesses
where IsValid = 1 and AccessID = 1
I want to convert this to a Linq query. I tried the following query but it is not working
List<Access> Accesses = _context.Accesses.ToList();
var query = from acc in Accesses
where acc.IsValid = 1 &&
acc.AccessID = v
select acc.A
I am having trouble near the && operator
For equality you need to do: == not =
For more details please refer: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/equality-comparisons#value-equality
List<Access> Accesses = _context.Accesses.ToList();
var query = from acc in Accesses
where acc.IsValid == 1 &&
acc.AccessID == 1
select acc.AccessName
Also I don't know what v is meant to be, variable or some value - I've changed that to 1, as per your original query, likewise acc.A I assumed it should be acc.AccessName.

Linq and String.Compare - the left hand side of an assignment must be a variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to write the following LINQ query...
string userSearch = textBoxSearchUserInput.Text;
//LINQ query for Member name search
var MemberNameSearch =
from member in context.Members
where String.Compare(userSearch, member.MemberLastName, true) = 0
select member;
But I'm getting the error message in the title.
What I'm trying to achieve, comparing two strings while ignoring the case
Thanks
where String.Compare(userSearch, member.MemberLastName, true) == 0
To compare you need to use ==
This would work
String.Equals(userSearch, member.MemberLastName, StringComparison.OrdinalIgnoreCase)
You should change it
where String.Compare(userSearch, member.MemberLastName, true) = 0
To
where String.Compare(userSearch, member.MemberLastName, true) == 0

compare the month part with linq [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to compare the month part of two values in linq but im getting an error
property or indexer DateTime.Month cannot be assigned to
Here's the code
var item = model.Where(d => d.DATE2.Date.Month = dto.StartDate.Month).FirstOrDefault();
model is a list of objects with datetime properties and the dto.Startdate is also a datetime
You may not come up with this question if you read the error message carefully, since its clear enough. Anyway, you have to use == here for comparing two values, = is for assignment purpose(basic knowledge). After making this small change your query will look like the following:
var item = model.Where(d => d.DATE2.Date.Month == dto.StartDate.Month).FirstOrDefault();
You can make them even simple by using FirstOrDefault instead for Where as like this:
var item = model.FirstOrDefault(d => d.DATE2.Date.Month == dto.StartDate.Month);

How to convert List<decimal> to List<string>? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My question is part of this problem:
I recieve a collection of id's from a form. I need to get the keys, convert them to string and pass it to the db.
using (var db = new DbAmsecEntities())
{
cashsafelist = (from safe in db.Cashsafes
where safe.StoreId == (decimal)Convert.ToInt64(ddlLocationLists.SelectedValue)
select safe.CashsafeId).ToList();
cashsafevalues = cashsafelist.Select(x => x.ToString).ToList();//getting error here
}
You need to use () in ToString
cashsafevalues = cashsafelist.Select(x => x.ToString()).ToList();
You can also use List.ConvertAll which was available since 2.0.
cashsafevalues = cashsafelist.ConvertAll<string>(d => d.ToString());
It can be more efficient than ToList because the list will be initialized with the correct size.

Categories

Resources