【问题标题】:Convert from sql to linq in MVC C# [closed]在 MVC C# 中从 sql 转换为 linq [关闭]
【发布时间】:2014-08-25 13:52:16
【问题描述】:

我无法从 sql 转换为 linq。我已经附上了我的 sql 查询和模型。

注意:我们需要以国家和目的地开头,并且使用 where 条件将国家 ID 传递到目的地。

SQL:

select Countries.Name,Destinations.DestinationName,Destinations.DestinationID 
from Destinations
join Countries on Destinations.CountryID = Countries.Id 
where
  Countries.Name like '%Sa%' or Destinations.DestinationName like '%sa%'

代码:

public class Destination
    {
        [Key]
        public virtual Guid Id { get; set; }

        public virtual string DestinationID { get; set; }

        public virtual string DestinationName { get; set; }

        public virtual Guid CountryID { get; set; }

        public virtual string State { get; set; }

        public virtual IEnumerable<HotelDetails> HotelDetails { get; set; }
    }

 public class Country : Entity
    {
       //Comment
        public virtual IEnumerable<Destination> Destinations { get; set; }



    }



 var Context = new HotelDbContext();

            var csdf = (from country in Context.Countries.Where(x =>x.Name.StartsWith(desti))
                        from destina in Context.Destinations.Where(x => x.DestinationName.StartsWith(desti))


                        select new
                        {
                            country.Name,
                            destina.DestinationName,
                            destina.DestinationID
                        }
                          );

【问题讨论】:

  • 这里有问题吗?

标签: c# sql asp.net-mvc linq


【解决方案1】:

你的 sql to linq 看起来像这样:

var csdf = (from d in Context.Destinations
            join c in Context.Countries on d.CountryId equals c.Id
            where
            c.Name.Contains("Sa") && d.DestinationName.Contains("sa")
            select new {
              c.Name,
              d.DestinationName,
              d.DestinationId
            });

【讨论】:

    【解决方案2】:

    你可以试试这样的:

    var result = context.Countries
        .Where
        (
            x=>
            x.Name.Contains('Sa') &&
            x.Destinations.Any(d=>d.DestinationName.Contains('sa'))
        )
        .Select
        (
            x=> new Country
            {
                Name = x.Name,
                Destinations = x.Destinations
                    .Where(d=>d.DestinationName.Contains('sa'))
                    .Select
                    (
                        d=>new Destination
                        {
                            Id = d.Id,
                            DestinationID = d.DestinationID ,
                            DestinationName = d.DestinationName ,
                            CountryID = d.CountryID ,
                            State = d.State
                        }
                    )
            }
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-13
      • 2018-07-31
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      相关资源
      最近更新 更多