LINQ选择位置,然后连接位置

0lvr5msh  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(175)

我试图从两个不同的表中确定在某个时间使用的指定车辆的计数。表postThr包含时间范围,表postFiv包含各种信息,包括当列“v”被指示时的车辆类型。
所有表共享一个“FK”列。
我们将获得日期、开始时间、结束时间和车辆类型。
我很确定我得到了下面的select语句。现在我必须加入postFiv来获得车辆。

DateTime givenDate = DateTime.Parse("11/15/22");
        TimeSpan givenTime = TimeSpan.Parse("9:00");
        TimeSpan givenEndT = TimeSpan.Parse("13:00");
        string vehicleType = "VAN";

        var qry = from x in _context.PostThrs
                  where
                    x.ThrDate == givenDate &&
                    x.ThrText == "SERVICE-" &&
                    ((
                    x.ThrTime < givenTime &&
                    x.ThrEndT > givenTime
                    ) || (
                    x.ThrTime < givenEndT &&
                    x.ThrEndT > givenEndT
                    ))
                    
                    select x
                    ;

我的直觉是继续说下去:

select x new {
join ex in _context.PostFivs
on ex.FK equals x.FK
where ex.Code == "V" && ex.Text == "VAN"
}.Count()

但我知道这是不对的,我真的很难处理复杂的LINQ语句;让我想回去学习SQL。
编辑
已请求模型类。

PostThr
-ThrID - int
-ThrFK - string
-ThrDate - datetime
-ThrTime - timespan
-ThrEndT - timespan
-ThrText - string

PostFiv
-FivID - int
-FivFK - string
-FivCode - string[1 char]
-FivText - string

编辑我不知道你要找的是什么,但这些是完整的模型。按照要求。没有导航属性。
这是postThr

public class PostThr
{
    //would be nice if we could require at least 1 record for each Zero record

    [Key]
    public int ThrId { get; set; }

    [ForeignKey("PostZero")]
    [Display(Name = "0/")]
    public string ThrZero { get; set; }

    [Display(Name = "Date*")]
    [DataType(DataType.Date)]
    //[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime ThrDate { get; set; }

    [Display(Name = "Time")]
    //[RegularExpression(@"^\d+\.\d{0,2}$", ErrorMessage = "Must be between 0.00 and 23.99")]
    //[Range(0, 23, ErrorMessage = "Must be between 0.00 and 23.99")]
    public TimeSpan ThrTime { get; set; }

    [Display(Name = "End Time")]
    public TimeSpan ThrEndT { get; set; }

    [Required]
    [RegularExpression(@"[a-zA-Z0-9""'\s-|\.\=\+\*\/\\]*$")]
    [Display(Name = "Text")]
    [StringLength(160, MinimumLength = 2)]
    public string ThrText { get; set; }

    public string ThrTrackUser { get; set; }

    public string ThrTrackTime { get; set; }
}

和后五年

public class PostFiv
{
    [Key]
    public int FivId { get; set; }

    [ForeignKey("PostZero")]
    [Display(Name = "0/")]
    public string FivZero { get; set; }

    [Required]
    [Display(Name = "Priority Number")]
    [StringLength(1, MinimumLength = 1)]
    //[RegularExpression(?)] need to have only numbers 1-9
    public string FivPrio { get; set; }

    [Required]
    [Display(Name = "Code Letter")]
    [StringLength(1, MinimumLength = 1)]
    //[RegularExpression(?)] need to have only letters a-z any case
    public string FivCode { get; set; }

    [Required]
    [RegularExpression(@"[a-zA-Z0-9""'\s-|\.\=\+\*\/\\/¥():]*$")]
    [Display(Name = "Remark Text")]
    [StringLength(2000, MinimumLength = 1)]
    public string FivText { get; set; }

    public string FivTrackUser { get; set; }

    public string FivTrackTime { get; set; }
}

编辑另一个咬苹果...有2个工作14月14日与货车,并符合标准的一般。所以答案必须是'2',但我得到零,所以,不,这是行不通的。

DateTime givenDate = DateTime.Parse("11/14/22");
        TimeSpan givenTime = TimeSpan.Parse("9:00");
        TimeSpan givenEndT = TimeSpan.Parse("18:00");

        var qry = from x in _context.PostThrs
                  where
                    x.ThrDate == givenDate &&
                    x.ThrText == "SERVICE-" &&
                    ((
                    x.ThrTime < givenTime &&
                    x.ThrEndT > givenTime
                    ) || (
                    x.ThrTime < givenEndT &&
                    x.ThrEndT > givenEndT
                    )) select new { zero = x.ThrZero };

        var ans = from y in qry join ex in _context.PostFivs on y.zero equals ex.FivZero select ex;

        var oth = (from z in ans where z.FivCode == "V" && z.FivText == "VAN-" select z).Count();

        var xyz = oth;
3b6akqbq

3b6akqbq1#

您可以在单一查询中使用直接连接,如下所示:

DateTime givenDate = DateTime.Parse("11/14/22");
    TimeSpan givenTime = TimeSpan.Parse("9:00");
    TimeSpan givenEndT = TimeSpan.Parse("18:00");

    var qry = from x in _context.PostThrs
              join y in _context.PostFivs on x.ThrZero equals y.zero
              where
                x.ThrDate == givenDate &&
                x.ThrText == "SERVICE-" &&
                ((
                x.ThrTime < givenTime &&
                x.ThrEndT > givenTime
                ) || (
                x.ThrTime < givenEndT &&
                x.ThrEndT > givenEndT
                ) && y.FivCode == "V" && y.FivText == "VAN-" ) select y.Count();

该查询将返回整个PostFivs数据。

相关问题