assignedtechsList 데이터 테이블에 포함되어있는 경우 내가 freeTechslist 데이터 테이블에서 중복 항목을 삭제하려고 내 코드를 도와 줄 수있는 데이터 테이블 Linq의 역할에 대해 설명하십시오. GetAllUsers
및 GetAllAssignedUserToTask
의 구현은 설명의 목적으로 중요하지 않습니다. 난 그냥 코드에서 사용하는 것처럼 DataTable을 반환하는 샘플 함수를 만들었습니다. 또한 Guids를 기술자의 식별자로 사용하도록 코드를 수정했습니다.
void Main()
{
var allTechs = GetAllUsers();
var freeTechsList =
//Start the Linq query by selecting all the available techs.
//Since GetAllUsers returns a DataTable, we use AsEnumerable
//to convert the DataTable to a queryable object
from tech in allTechs.AsEnumerable()
//Now we add a where clause to the Linq query. Essentially
//what this clause does is just remove every tech that is in
//the assigned techs list (which is returned by GetAllAssignedUserToTask)
where
!(
//This is our subquery; all this does is give us a list of Guids
//that identifies techs that are assigned. Again, GetAllAssignedUserToTask
//returns a DataTable, so we use AsEnumerable to make it queryable with Linq
from assigned in GetAllAssignedUserToTask(allTechs).AsEnumerable()
select assigned.Field<Guid>("TechGUID")
//The above subquery has returned us a list of assigned techs, so we check if
//the current tech we're looking at from GetAllUsers is in that list using
//the Contains method. Contains will tell us whether the current tech is in the
//list of assigned techs. Since we negate the result above (using !), that means
//that if the tech is in this list, he will not be included in the final result.
).Contains(tech.Field<Guid>("TechGUID"))
//Finally, we return the selected techs that have been filtered by our where clause
select tech;
//Dump is basically just LinqPad's version of print so we can see the result.
freeTechsList.Dump();
}
// Define other methods and classes here
DataTable GetAllUsers()
{
//create a datatable
var dt = new DataTable();
dt.Columns.Add(new DataColumn() {DataType = typeof(Guid), ColumnName = "TechGUID"});
dt.Columns.Add(new DataColumn() {DataType = typeof(String), ColumnName = "Name"});
//add some data
DataRow bob = dt.NewRow();
bob["Name"] = "Bob";
bob["TechGUID"] = Guid.NewGuid();
dt.Rows.Add(bob);
DataRow phil = dt.NewRow();
phil["Name"] = "Phil";
phil["TechGUID"] = Guid.NewGuid();
dt.Rows.Add(phil);
DataRow joe = dt.NewRow();
joe["Name"] = "Joe";
joe["TechGUID"] = Guid.NewGuid();
dt.Rows.Add(joe);
return dt;
}
DataTable GetAllAssignedUserToTask(DataTable allUsers)
{
//simulate Joe as our only assigned Tech
return allUsers.AsEnumerable()
.Where(tech => tech.Field<String>("Name") == "Joe")
.CopyToDataTable();
}
ID/키처럼 작동하는 열이 있습니까? 그렇다면 이름이 어떻게 붙여 집니까? –
Linq 및 Distinct를 사용하면 도움이되지 않습니까? – Rumplin