简体   繁体   中英

convert sql statement to linq (join two tables with where condition)

i am a bigneer in linq in c# and tried a lot in converting the following statement to linq but failed could any one help me to

SELECT        dbo.OFM_OffsetWells_studio.WellNames, dbo.OFM_WellActivity_studio.Date, 
dbo.OFM_WellActivity_studio.Activity, dbo.OFM_WellActivity_studio.ActivityRtf, 
dbo.OFM_WellActivity_studio.Type, 
                     dbo.OFM_WellActivity_studio.Diff, 
dbo.OFM_WellActivity_studio.Report, dbo.OFM_WellActivity_studio.LastModifiedBy, 
dbo.OFM_WellActivity_studio.LastModifiedDate
FROM            dbo.OFM_WellActivity_studio INNER JOIN
                     dbo.OFM_OffsetWells_studio ON dbo.OFM_WellActivity_studio.Alias = 
dbo.OFM_OffsetWells_studio.RubbishWellNames
WHERE        (dbo.OFM_OffsetWells_studio.WellNames = N'ZZ-04')

models are like following

public class WellActivity
{
    public Guid Id { get; set; }
    public Guid? headerId { get; set; }
    public string Alias { get; set; }
    public string Type { get; set; }
    public decimal? Diff { get; set; }
    public bool? Report { get; set; }
    public DateTime Date { get; set; }
    public string Activity { get; set; }
    public byte[] ActivityRtf { get; set; }
    public string LastModifiedBy { get; set; }
    public DateTime? LastModifiedDate { get; set; }

}


 public class OffsetWells
{
    public Guid Id { get; set; }
    public string RubbishWellNames { get; set; }
    public string WellNames { get; set; }

}

Try following :

class Program
{
    static void Main(string[] args)
    {
        Context db = new Context();
        var results = from wa in db.WellActivity
                      join ow in db.OffsetWells.Where(x => x.WellNames == "ZZ-04") on wa.Alias equals ow.RubbishWellNames
                      select new {
                         wellNames = ow.WellNames};
    }
}

public class Context
{
    public List<WellActivity> WellActivity { get; set; }
    public List<OffsetWells> OffsetWells { get; set; }
}

public class WellActivity
{
    public Guid Id { get; set; }
    public Guid? headerId { get; set; }
    public string Alias { get; set; }
    public string Type { get; set; }
    public decimal? Diff { get; set; }
    public bool? Report { get; set; }
    public DateTime Date { get; set; }
    public string Activity { get; set; }
    public byte[] ActivityRtf { get; set; }
    public string LastModifiedBy { get; set; }
    public DateTime? LastModifiedDate { get; set; }
}

public class OffsetWells
{
    public Guid Id { get; set; }
    public string RubbishWellNames { get; set; }
    public string WellNames { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM