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);
Related
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);
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 3 years ago.
Improve this question
SML is a property of regelSML. I want to set regelSML to a value given by Reader.getString(i).
if(reader.GetName(i) = "SML") regelSML.SML = reader.GetString(i);
As clarification, like Guy wrote in the comments above, you are not doing a comparison in the condition.
A single = is for assignments.
You need the comparison-operator ==.
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
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
I'm using LINQ to read in some XML and then use that to set properties in an object. The XML looks like this:
<display_location>
<full>London, United Kingdom</full>
<city>London</city>
<state/>
<state_name>United Kingdom</state_name>
<country>UK</country>
<country_iso3166>GB</country_iso3166>
<zip>00000</zip>
<magic>553</magic>
<wmo>03772</wmo>
<latitude>51.47999954</latitude>
<longitude>-0.44999999</longitude>
<elevation>24.00000000</elevation>
</display_location>
And the code I have is:
select new Forecast
{
//Set properties for the display location
DisplayLatitude = (double)i.Element("display_location").Element("latitude"),
DisplayLongtitude = (double)i.Element("display_location").Element("longtitude"),
DisplayElevation = (string)i.Element("display_location").Element("elevation"),
};
I can correctly set the latitude and elevation however I'm getting the exception "Value cannot be null" when I try to parse the longtitude.
I think it might be because of the negative symbol. How do I fix this?
It's "longitude", not "longtitude". Your string needs to exactly match the XML element name or you'll get a null value instead.
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.