In the code below I have a problem:
List<SurveyContext> surveys = (from s in DbContext.Surveys
where s.userName == s.userName
select s.ID).ToList();
if(surveys.Count() > 0)
{
int id = 0;
int.TryParse(Surveys.First(), id);
return id;
}
return 0;
Error : ToList()
Error 1 A local variable named 's' cannot be declared in this scope because it would give a different meaning to 's', which is already used in a 'parent or current' scope to denote something else
and here:
int.TryParse(Surveys.First(), id);
The name 'Surveys' does not exist in the current context
But this name exist in context... Can anyone help me??
Error 1
Use == instead of =
where s.userName == userName
Error 2
Try naming s variable on your linq query in a different way, maybe su or survey. But it seems to me that error is on code that you're not showing.
List<SurveyContext> surveys = (from su in DbContext.Surveys
where su.userName == su.userName
select su.ID).ToList();
Error 3
I think you meant surveys instead of Surveys and you need to use out when passing idparam
int.TryParse(surveys.First(), out id);
And since you're not evaluating parse result I'd say you should use int.Parse
id = int.Parse(surveys.First());
And you have to use a property of survey, you're trying to convert the entire survey instance to int
id = int.Parse(surveys.First().Property);
Related
I am having a Semester1 including StartDate1 and EndDate1. I want to create a new Semester2 and it must ensure EndDate1 < StartDate2. How can I compare them?
I am using .Net Core 5 and Repository pattern
If you have any guidance please let me know. And if you need more information about the Code please tell me.
Repo The comparisons below are made by me inside a semester
public SemesterViewModel CreateSemester(Semester semester)
{
if (Semester.SemesterStartDate <Semester.SemesterClosureDate &&
Semester.SemesterClosureDate < Semester.SemesterEndDate)
{
var newSemester = new Semester
{
SemesterName = Semester.SemesterName,
SemesterStartDate = Semester.SemesterStartDate,
SemesterClosureDate = Semester.SemesterClosureDate,
SemesterEndDate = Semester.SemesterEndDate
};
_dbContext.Semesters.Add(newSemester);
_dbContext.SaveChanges();
return null;
}
So after reviewing your repo. i am updating my answer. I can see that you're trying to compare the dates between semester 1 and semester 2. But here you're just comparing dates of semester 2 which won't work.
You should first get semester1 from db and then compare the date with that.
var semester1 = GetSemester(); //get semester 1;
So if you want to compare with last semester than:
var semester1 = Get().OrderByDescending(a => a.Id).FirstOrDefault();
and then:
if (semester1 == null || (semesterViewModel.Semester.SemesterStartDate > semester1.SemesterEndDate))
{
//save newSemester
}
I would like to retrieve the userInfoId from the database and pass it into var where the Login username is Kenny. However, the value returned to u was 0 instead of the desired value. I have tried selecting another username but the result was still the same.
var u = Database.UserInfo
.Where(userinfo => userinfo.LoginUserName == "BEN")
.Select(x=> x.UserInfoId)
.FirstOrDefault();
Put some breakpoints and see what you have inside u, Use the query below and you should be good to go. Make sure the table/column names are correct according to your db.
int userInfoId = (from x in context.UserInfo
where x.LoginUserName == "Kenny"
select x.UserInfoId).SingleOrDefault());
if (userInfoId > 0){
// user exists and do what you wish to next
}
else {
// user does not exist
}
var query = from c in context.Albums
where c.AlbumID in albumIds
select c.Albumname;
Here albumIds is IENUM<> of some custom type(in my case its an Entity)
When I do the above query I get an error
Operator == cannot be applied to type int and IEnumerable
This error is acceptable but how do I overcome this
Turn it around slightly - you want to check whether your enumerable of album ids contains the id of an album you have just iterated to in your linq. So something like this ...
albumIds = {1,2,13,25,277,567};
var query = context.Albums.Where(x=> albumIds.Contains(x.ID));
(Sorry, writing example code without a tool in front of me so forgive any obvious mistakes. Hopefully you can get the idea from this though).
var albumIds= new string[] { "900", "801", "802", "803","906" };
var lstData = context.tbl.Where(
x => (x.TimeCreated >= yesterday && x.TimeCreated < today) &&
albumIds.Contains(x.TransactionSetId)
).Select(x => x.X12_Interchange).ToList();
Im having an issue with this , Im new to asp.net and really dont have much understanding with their syntax, I do understand the error but have no idea how to implement a fix:
ProductTable.RowFilter = "ProductID ='" & ddlCategory.SelectedValue & "'"
The error im recieving is : cannot perform '=' operation on system.int32 and system.string data view
my productID data type is : int;
Now i dont see how im comparing a string vs int, ddlCatergory selected index would also return a value... besides the point...im completely stuck.
any help is appreciated.
Using windows 8, VS 2012 , SQL database.
ddlCategory.SelectedValue is likely to be string given the SelectedValue attribute of the drop down list is a string, you are also assigning it as a string e.g. the filter value could end up being something like "ProductID ='123'" where 123 is the SelectedValue.
Assuming that SelectedValue is never null or empty I would use something like:
ProductTable.RowFilter = string.format("ProductID = {0}", ddlCategory.SelectedValue);
If it could be empty then I would do something along these lines:
if(string.IsNullOrEmpty(ddlCategory.SelectedValue))
{
ProductTable.RowFilter ="";
}
else {
ProductTable.RowFilter = string.format("ProductID = {0}", ddlCategory.SelectedValue);
}
OR
int tempInt;
if(int.TryParse(ddlCategory.SelectedValue,out tempInt))
{
ProductTable.RowFilter = string.format("ProductID = {0}", tempInt);
}
I've written the above in C# (not syntax checked though sorry!) though it would be easy enough to convert it to VB if necessary.
You should try by removing single quotes after = sign, so it give something like this
ProductTable.RowFilter = "ProductID = " + ddlCategory.SelectedValue
I have the code below where I am trying to go through the child questions of my qestAires anonymous type. When I get to the foreach loop i get the error:
foreach statement cannot operate on
variables of type 'Question' because
'Question' does not contain a public
definition for 'GetEnumerator'
What do I need to do differently to get around this?
var questAires = (from qs in dc.Questionnaires
from q in dc.Questions.Where(t => t.QuestionnaireId == qs.QuestionnaireID)
from r in dc.Responses.Where(qr => qr.QuestionID == q.QuestionId).DefaultIfEmpty()
where qs.QuestionnaireID == QuestionnaireId
select new
{
qs.Description,
Questions = q,
Responses = r
}).Single();
foreach(var question in questAires.Questions)
{
}
questAires.Questions will only resolve to a single question, and you will get one questAires object for each question (which will cause .Single() to throw).
I guess you want something like this:
var questAires = (from qs in dc.Questionnaires
select new {
qs.Description,
Questions = from q in dc.Questions where q.QuestionnaireId == qs.QuestionnaireID
select new {
Question = q,
Response = (from r in dc.Responses where r.QuestionID == q.QuestionId select r).DefaultIfEmpty()
}
}).Single()
q actually resolves to a single item from the enumerable dc.Questions.Where(...), so yeah, you're only going to get a single item - not an enumerable - for Questions.