17

I have a little misunderstanding here why do i have here an error do i need to parse it what is wrong with this code ?

UberTrackerEntities ctx = UberFactory.Context;
IEnumerable<HtUser> users = HtUser.GetAll();
string selectedBU = rcbBusinessUnits.SelectedValue;
string selectedDepartment = rcbDepartment.SelectedValue;

HtDepartment department = ctx.HtDepartments.SingleOrDefault(d => d.DepartmentId ==selectedDepartment);

if (department != null) 
{
    users = users.Where(u => u.HtDepartments.Contains(department));
}

Thanks for help and fast answer !

PS:I thing I'm just over thing it it seams just to be a stupid little error ...

4
  • 5
    The error here is pretty clear. One of d.DepartmentId and selectedDepartment is an int, and the other a string, and you can't compare the two with ==. Commented Mar 14, 2013 at 9:06
  • you want Microsoft people should include Which variable causing error. ? it's pretty informative. you can get the problematic code by looking it. Commented Mar 14, 2013 at 9:08
  • Give more details: what are you trying to do ? how are you doing it ? what was expected ? And what was the output which is different to your expectation ? without such info. no one can help you. Commented Mar 14, 2013 at 9:08
  • @lc. how can i do it correctly ? Commented Mar 14, 2013 at 9:18

3 Answers 3

15

You need to convert selectedDepartment to integer before comparing it in the LINQ query.

int selectedDepartment = Convert.ToInt32(rcbDepartment.SelectedValue);

In your query:

ctx.HtDepartments.SingleOrDefault(d => d.DepartmentId == selectedDepartment);

d.DepartmentId is of type int whereas selectedDepartment is a string and you can compare both using == operator.

Sign up to request clarification or add additional context in comments.

4 Comments

Input string was not in a correct format. can you please edit your answer ?
@MCraft, put a breakpoint on the line and see what you get inside rcbDepartment.SelectedValue, it must be something other than integer value
Get the value of the currently selected item in the combobox
@MCraft, what is the selected value
0

You selectedDepartment is of type string, and your id is of type int. You should convert your selectedDepartment to a int:

int selectedDepartment = Convert.ToInt32(rcbDepartment.SelectedValue);

1 Comment

Then you should set the values of the rcbDepartment.SelectedValue to the keys of your departments. Eighter by hand or by databinding to the correct key.
0

d.DepartmentId is an int and selectedDepartment is a string.

You will need to convert using Int32.Parse, Int32.TryParse or Convert.ToInt32

Edit:

int selectedDepartmentId = Convert.ToInt32(selectedDepartment);

HtDepartment department = ctx.HtDepartments.SingleOrDefault(d => d.DepartmentId == selectedDepartmentId));

3 Comments

I have still a problem here LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.
@MCraft - Updated my answer. Try that.
@MCraft - selectedDepartment must not be a valid integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.