Как я могу решить эти проблемы (linq to SQL)
Я новичок в программировании. Я пытаюсь добиться того, чтобы получить следующие вопросы (которые выделены жирным шрифтом). Какие изменения мне нужно внести в свои коды для их достижения?
My code: public static int CountEmployeesByName(string[] namesToSearch) { using (ApplicationEntities context = new ApplicationEntities()) { var queries = new List<IEnumerable<employee>>(); string nameParam; foreach (string name in namesToSearch) { nameParam = name; //search for employees by name queries.Add((from e in context.employees where e.name == nameParam select e)); } return queries.Sum(q => q.Count()); } } public static void UnitTests() { //Populate with test data using (ApplicationEntities context = new ApplicationEntities()) { context.Database.ExecuteSqlCommand(@" TRUNCATE TABLE employees INSERT INTO employees (name) VALUES ('Niño'); INSERT INTO employees (name) VALUES (N'?????'); SET ANSI_WARNINGS OFF; INSERT INTO employees (name) VALUES ('VeryLongName'); INSERT INTO employees (name) VALUES ('Name1'); INSERT INTO employees (name) VALUES ('Name2'); INSERT INTO employees (name) VALUES ('Nick');"); } //Question //The function is always failing to find any but the last name in the namesToSearch array. Debug.Assert(CountEmployeesByName(new string[] { "Name1", "Name2", "NoName3" }) == 2); //It fails . it finds 0 why? Shouldnt it return 2? What do i make change in my code? //Question //Every matching employee record is being retrieved from the database server (very inefficient). //The SQL engine should do the counting. Debug.Assert(CountEmployeesByName(new string[] { "Nick", "Name1" }) == 3); //Fails.. it counts only 2, Why? /* //Question //What changes sdo i make in The function so that it should not be running a query for each //search name. It should run exactly 1 SQL statement each time the function is called. Debug.Assert(CountEmployeesByName(new string[] { "Nick", "Name1" }) == 3); //Question //One employee can be counted twice. It should return number of unique employees. Debug.Assert(CountEmployeesByName(new string[] { "Name1", "name1" }) == 1); //it finds 2, what changes do i make ? } static void Main(string[] args) { UnitTests(); }