C# LINQ查询中的错误
我对此查询有问题:C# LINQ查询中的错误,c#,linq,C#,Linq,我对此查询有问题: var res = from c in db.Client where db.TimesheetLine.Select(o => o.ClientId && o.TimesheetId == timesheetId) .Contains(c.Id) select
var res = from c in db.Client
where db.TimesheetLine.Select(o => o.ClientId &&
o.TimesheetId == timesheetId)
.Contains(c.Id)
select c;
我收到了错误消息:
运算符“&&”不能应用于“int”和“bool”类型的操作数
我要做的就是选择时间表X中的所有客户端
我有client类和timesheetline类
public class CLient
{
public int Id { get; set; }
public string ClientName { get; set; }
}
public class TimesheetLine
{
public int TimesheetLineId { get; set; }
public int TimesheetId { get; set; }
public System.DateTime Date { get; set; }
public double Duration { get; set; }
public Nullable<int> ClientId { get; set; }
public Nullable<int> ClientAddressId { get; set; }
public int ActivityTypeId { get; set; }
public string Overtime { get; set; }
public string Description { get; set; }
public virtual Activity Activity { get; set; }
public virtual Client Client { get; set; }
public virtual ClientAddress ClientAddress { get; set; }
public virtual Timesheet Timesheet { get; set; }
}
公共类客户端
{
公共int Id{get;set;}
公共字符串ClientName{get;set;}
}
公共类时间表
{
public int TimesheetLineId{get;set;}
公共int时间表{get;set;}
public System.DateTime日期{get;set;}
公共双持续时间{get;set;}
公共可为空的ClientId{get;set;}
公共可为空的ClientAddressId{get;set;}
public int ActivityTypeId{get;set;}
公共字符串超时{get;set;}
公共字符串说明{get;set;}
公共虚拟活动活动{get;set;}
公共虚拟客户端{get;set;}
公共虚拟客户端地址客户端地址{get;set;}
公共虚拟时间表时间表{get;set;}
}
尝试使用不同的查询:
var res = (from ts in db.TimesheetLine
where ts.TimesheetId == timesheetId)
select ts.Client).ToList();
遍历TimsheetLine数据,选择与特定时间表关联的客户机
您可以执行空检查,以防:
var res = (from ts in db.TimesheetLine
where ts.TimesheetId == timesheetId && ts.Client != null)
select ts.Client).ToList();
您可以创建一个
join
var res = from client in db.Client
join timesheetLine in db.TimesheetLine
on client.Id equals timesheetLine.ClientId.GetValueOrDefault()
where timesheetLine.timesheetId == timesheetId
select client;
这里有趣的是,我们希望将联接基于可为空的int、
timesheetLine.ClientId
和int、client.Id
的值。为了做到这一点,我们使用GetValueOrDefault
方法,当可空值没有值时,该方法返回可空值的默认值。在这种情况下,将返回的值为0假设没有任何客户机具有Id
以上功能。不同的是,我们必须对其进行一些更改。您不能在Select
中编写条件语句。您可以改用任何:
var res = from c in db.Client
where db.TimesheetLine.Any(o => o.ClientId == c.Id)
select c;
o.ClientId
不返回布尔值。是的,编译器会告诉您该错误消息的所有混淆之处?基于“我要做的就是选择时间表线X中的所有客户端”,这是我的答案(注意,我可以看到其他的不是无效的LINQ查询,但我认为这一个符合您的目标)客户端
没有时间表
,时间表线
有导航属性。@mo.esmp oops您是对的。您的帖子再次编辑,我没有注意到最初的帖子。请几分钟后查看我的更新。谢谢:)