C# 如何通过linq获取此数据 公共类员工 { 公共int ID{get;set;} 公共字符串名称{get;set;} } 公共类登录 { 公共int ID{get;set;} 公共日期时间登录时间{get;set;} } 静态void Main(字符串[]参数) { List AllEmployees=新列表() { 新员工(){ID=1,Name=“John”}, 新员工(){ID=2,Name=“Pete”}, 新员工(){ID=3,Name=“Sue”}, 新员工(){ID=4,Name=“Chris”}, 新员工(){ID=5,Name=“Kate”}, 新员工(){ID=6,Name=“Smith”}, 新员工(){ID=7,Name=“Bil”}, 新员工(){ID=8,Name=“Shan”}, 新员工(){ID=9,Name=“Joe”}, 新员工(){ID=10,Name=“Lure”} }; List SomeEmployeesIDs=新列表() { 1, 2, 5, 6, 10 }; List EmployeeLogins=新列表() { new LoginAudit(){ID=1,LoginTime=new DateTime(2015,1,11)}, new LoginAudit(){ID=5,LoginTime=new DateTime(2015,2,2)} }; }
我想从所有员工中选择所有符合以下条件的员工。C# 如何通过linq获取此数据 公共类员工 { 公共int ID{get;set;} 公共字符串名称{get;set;} } 公共类登录 { 公共int ID{get;set;} 公共日期时间登录时间{get;set;} } 静态void Main(字符串[]参数) { List AllEmployees=新列表() { 新员工(){ID=1,Name=“John”}, 新员工(){ID=2,Name=“Pete”}, 新员工(){ID=3,Name=“Sue”}, 新员工(){ID=4,Name=“Chris”}, 新员工(){ID=5,Name=“Kate”}, 新员工(){ID=6,Name=“Smith”}, 新员工(){ID=7,Name=“Bil”}, 新员工(){ID=8,Name=“Shan”}, 新员工(){ID=9,Name=“Joe”}, 新员工(){ID=10,Name=“Lure”} }; List SomeEmployeesIDs=新列表() { 1, 2, 5, 6, 10 }; List EmployeeLogins=新列表() { new LoginAudit(){ID=1,LoginTime=new DateTime(2015,1,11)}, new LoginAudit(){ID=5,LoginTime=new DateTime(2015,2,2)} }; },c#,linq,C#,Linq,我想从所有员工中选择所有符合以下条件的员工。 1.在一些员工ID中有ID和 2.要么根本没有登录(意思是在员工登录中没有条目),要么上次登录时间超过7天) 基于此,我应该得到ID为1、5、6和10的员工。有人能帮我写Linq查询来检索这些数据吗?我想你需要这样的东西: public class Employee { public int ID { get; set; } public string Name { get; set; } } public class LoginA
1.在一些员工ID中有ID和
2.要么根本没有登录(意思是在员工登录中没有条目),要么上次登录时间超过7天)
基于此,我应该得到ID为1、5、6和10的员工。有人能帮我写Linq查询来检索这些数据吗?我想你需要这样的东西:
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
public class LoginAudit
{
public int ID { get; set; }
public DateTime LoginTime { get; set; }
}
static void Main(string[] args)
{
List<Employee> AllEmployees = new List<Employee>()
{
new Employee() { ID = 1, Name = "John"},
new Employee() { ID = 2, Name = "Pete"},
new Employee() { ID = 3, Name = "Sue"},
new Employee() { ID = 4, Name = "Chris"},
new Employee() { ID = 5, Name = "Kate"},
new Employee() { ID = 6, Name = "Smith"},
new Employee() { ID = 7, Name = "Bil"},
new Employee() { ID = 8, Name = "Shan"},
new Employee() { ID = 9, Name = "Joe"},
new Employee() { ID = 10, Name = "Lure"}
};
List<int> SomeEmployeesIDs = new List<int>()
{
1, 2, 5, 6, 10
};
List<LoginAudit> EmployeeLogins = new List<LoginAudit>()
{
new LoginAudit() { ID = 1, LoginTime = new DateTime(2015,1,11)},
new LoginAudit() { ID = 5, LoginTime = new DateTime(2015,2,2)}
};
}
var q = AllEmployees
.Where(emp => SomeEmployeesIDs.Contains(emp.ID)
&& !EmployeeLogins
.Any(el => el.ID == emp.ID
&& (DateTime.Now - el.LoginTime).Days <= 7));
我想你需要这样的东西:
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
public class LoginAudit
{
public int ID { get; set; }
public DateTime LoginTime { get; set; }
}
static void Main(string[] args)
{
List<Employee> AllEmployees = new List<Employee>()
{
new Employee() { ID = 1, Name = "John"},
new Employee() { ID = 2, Name = "Pete"},
new Employee() { ID = 3, Name = "Sue"},
new Employee() { ID = 4, Name = "Chris"},
new Employee() { ID = 5, Name = "Kate"},
new Employee() { ID = 6, Name = "Smith"},
new Employee() { ID = 7, Name = "Bil"},
new Employee() { ID = 8, Name = "Shan"},
new Employee() { ID = 9, Name = "Joe"},
new Employee() { ID = 10, Name = "Lure"}
};
List<int> SomeEmployeesIDs = new List<int>()
{
1, 2, 5, 6, 10
};
List<LoginAudit> EmployeeLogins = new List<LoginAudit>()
{
new LoginAudit() { ID = 1, LoginTime = new DateTime(2015,1,11)},
new LoginAudit() { ID = 5, LoginTime = new DateTime(2015,2,2)}
};
}
var q = AllEmployees
.Where(emp => SomeEmployeesIDs.Contains(emp.ID)
&& !EmployeeLogins
.Any(el => el.ID == emp.ID
&& (DateTime.Now - el.LoginTime).Days <= 7));
您可以使用以下查询:
var result = AllEmployees.Where( x=> SomeEmployeesIDs.Contains(x.ID) &&
((!EmployeeLogins.Any(y=>y.ID==x.ID) ||
EmployeeLogins.Any(y=> y.ID==x.ID &&
(y.LoginTime-DateTime.Now).Days>7)));
您可以使用以下查询:
var result = AllEmployees.Where( x=> SomeEmployeesIDs.Contains(x.ID) &&
((!EmployeeLogins.Any(y=>y.ID==x.ID) ||
EmployeeLogins.Any(y=> y.ID==x.ID &&
(y.LoginTime-DateTime.Now).Days>7)));
使用如下查询:
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
public class LoginAudit
{
public int ID { get; set; }
public DateTime LoginTime { get; set; }
}
static void Main(string[] args)
{
List<Employee> AllEmployees = new List<Employee>()
{
new Employee() { ID = 1, Name = "John"},
new Employee() { ID = 2, Name = "Pete"},
new Employee() { ID = 3, Name = "Sue"},
new Employee() { ID = 4, Name = "Chris"},
new Employee() { ID = 5, Name = "Kate"},
new Employee() { ID = 6, Name = "Smith"},
new Employee() { ID = 7, Name = "Bil"},
new Employee() { ID = 8, Name = "Shan"},
new Employee() { ID = 9, Name = "Joe"},
new Employee() { ID = 10, Name = "Lure"}
};
List<int> SomeEmployeesIDs = new List<int>()
{
1, 2, 5, 6, 10
};
List<LoginAudit> EmployeeLogins = new List<LoginAudit>()
{
new LoginAudit() { ID = 1, LoginTime = new DateTime(2015,1,11)},
new LoginAudit() { ID = 5, LoginTime = new DateTime(2015,2,2)}
};
}
var q = AllEmployees
.Where(emp => SomeEmployeesIDs.Contains(emp.ID)
&& !EmployeeLogins
.Any(el => el.ID == emp.ID
&& (DateTime.Now - el.LoginTime).Days <= 7));
使用如下查询:
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
public class LoginAudit
{
public int ID { get; set; }
public DateTime LoginTime { get; set; }
}
static void Main(string[] args)
{
List<Employee> AllEmployees = new List<Employee>()
{
new Employee() { ID = 1, Name = "John"},
new Employee() { ID = 2, Name = "Pete"},
new Employee() { ID = 3, Name = "Sue"},
new Employee() { ID = 4, Name = "Chris"},
new Employee() { ID = 5, Name = "Kate"},
new Employee() { ID = 6, Name = "Smith"},
new Employee() { ID = 7, Name = "Bil"},
new Employee() { ID = 8, Name = "Shan"},
new Employee() { ID = 9, Name = "Joe"},
new Employee() { ID = 10, Name = "Lure"}
};
List<int> SomeEmployeesIDs = new List<int>()
{
1, 2, 5, 6, 10
};
List<LoginAudit> EmployeeLogins = new List<LoginAudit>()
{
new LoginAudit() { ID = 1, LoginTime = new DateTime(2015,1,11)},
new LoginAudit() { ID = 5, LoginTime = new DateTime(2015,2,2)}
};
}
var q = AllEmployees
.Where(emp => SomeEmployeesIDs.Contains(emp.ID)
&& !EmployeeLogins
.Any(el => el.ID == emp.ID
&& (DateTime.Now - el.LoginTime).Days <= 7));
不管谁投了反对票,你能告诉我原因吗。所以我可以从中吸取教训,可能是因为你没有展示你自己解决问题的尝试。我怀疑(不是我),因为你没有展示你尝试过的任何东西。你有标准,你知道你需要LINQ。你有没有试图解决这个问题?你搜索过相关主题吗?关于使用LINQ过滤数据的例子有上千个。我对LINQ是新手,我只是不知道如何使用LINQ和/或过滤数据。我没有找到任何例子,但我对林肯的了解还不够。不管谁投了反对票,你能告诉我原因吗。所以我可以从中吸取教训,可能是因为你没有展示你自己解决问题的尝试。我怀疑(不是我),因为你没有展示你尝试过的任何东西。你有标准,你知道你需要LINQ。你有没有试图解决这个问题?你搜索过相关主题吗?关于使用LINQ过滤数据的例子有上千个。我对LINQ是新手,我只是不知道如何使用LINQ和/或过滤数据。我没有找到任何例子,但我只是对林琦了解不够。我认为这是找到你的数据的最佳方式。我实际上对它投了较高的票,但我没有得到理想的结果。如果员工登录人数较大,则效率不高,因为你需要列举两次。我认为这是找到你的数据的最佳方式。我实际上对它投了较高的票,但我没有得到如果EmployeeLogins较大,则效果很好,因为您需要枚举它两次。只要
someEmployeeSID
是唯一的join
是个好主意。@PaulDetect:如果您坚持方法语法,我就避免join
。看一看。有方法语法的比有连接的性能差吗?@PaulDete:不,不太好<如果集合较大,则code>Join效率更高,但通常不会有什么不同。但正如Magnus所提到的,Join
将“创建”多行,如果某些员工ID
包含重复项.Where(emp=>SomeEmployeesIDs.Contains(emp.ID))
没有这个陷阱。只要SomeEmployeesIDs
是唯一的join
是个好主意。@pauldetect:如果你坚持方法语法,我就避免join
。看一看。有方法语法的比有连接的性能差吗?@PaulDete:不,不太好<如果集合较大,则code>Join效率更高,但通常不会有什么不同。但正如Magnus所提到的,Join
将“创建”多行,如果某些员工ID
包含重复项.Where(emp=>SomeEmployeesIDs.Contains(emp.ID))
没有这个陷阱。由于枚举EmployeeLogin两次,效率低下。因为枚举EmployeeLogin两次,效率低下。