Exception thrown when using a Linq query with Entity Framework

I deleted a significant part of the text from this question because I think it needs to be more concise. - Serg

Here is what I am trying to do. The user selects a state, and then the ComboBox loads with Cities in this state, then when the user selects City, the ListBox loads all the projects from this City.

When I run a method that should load projects into a ComboBox, I get this error:

An exception was chosen to target the call. Internal Exception: "{" The specified order is not valid. "}" is valid. "}"

This is the method that throws the exception:

private void LoadProjectsToListBox(long idCity)
{
    ProjectRepository projectRepo = new ProjectRepository();    

    //This line causes the error.
    var projects = projectRepo.FindAllProjects().Where(c => c.IDCity == 1).ToList();
}

Linq , . 1, , , . , Linq , . , , , .

SQL, Project City:

create table City
(
    ID integer primary key autoincrement,
    Name string,
    IDState integer references State(ID)
);

create table Project
(
    ID integer primary key autoincrement,
    Name string,
    StartDate text,
    IDManager integer references Manager(ID),
    IDCity integer references City(ID),
    IDDepartment integer references Department(ID),
    ContactNumber string,
    Description string
);

, , , ( ), , . , , Departments :

private void LoadProjectsToListBox(long idCity)
{
    ProjectRepository projectRepo = new ProjectRepository();
    DepartmentRepository departmentRepo = new DepartmentRepository();

    //Doesn't work - raises exception.
    var projects = projectRepo.FindAllProjects().Where(c => c.IDCity == 1).ToList();

    //Works exactly as expected.
    var deps = departmentRepo.FindAllDepartments().Where(c => c.IDParentDepartment == 7).ToList();

    lstProjects.Items.Clear();
    foreach (var item in deps)
    {
        lstProjects.Items.Add(item.Name);
    }
}

Edit:

FindAllDepartments(), FindAllCities(), FindAllProjects() .

DocumentsDBEntities db = new DocumentsDBEntities();

public IQueryable<Project> FindAllProjects()
{
    return db.Projects;
}

public IQueryable<City> FindAllCities()
{
    return db.Cities;
}

public IQueryable<Department> FindAllDepartments()
{
    return db.Departments;
}

, ; .

3:

! "" Project. "" "", . - , ?

+3
3

, , == ( OrderBy, ). .

+1

():

var projects = projectRepo.FindAllCities()
    .Where(c => c.ID == 1)
    .Select(p => p.Projects)
    .ToList();
0

! , , .dbml, ( ), () , 'db1.table1' 'db2.table1'. , , , . , , , .

0

Source: https://habr.com/ru/post/1772573/


All Articles