我遇到了用LINQ和Lambda编写的查询的麻烦。到目前为止,我得到了很多错误,这是我的代码:

int id = 1;
var query = database.Posts.Join(database.Post_Metas,
                                post => database.Posts.Where(x => x.ID == id),
                                meta => database.Post_Metas.Where(x => x.Post_ID == id),
                                (post, meta) => new { Post = post, Meta = meta });

我不确定这个问题是否正确。


我发现如果你熟悉SQL语法,使用LINQ查询语法会更清晰、更自然,并且更容易发现错误:

var id = 1;
var query =
   from post in database.Posts
   join meta in database.Post_Metas on post.ID equals meta.Post_ID
   where post.ID == id
   select new { Post = post, Meta = meta };

如果您真的坚持使用lambdas,那么您的语法就有很大的问题。下面是相同的查询,使用LINQ扩展方法:

var id = 1;
var query = database.Posts    // your starting point - table in the "from" statement
   .Join(database.Post_Metas, // the source table of the inner join
      post => post.ID,        // Select the primary key (the first part of the "on" clause in an sql "join" statement)
      meta => meta.Post_ID,   // Select the foreign key (the second part of the "on" clause)
      (post, meta) => new { Post = post, Meta = meta }) // selection
   .Where(postAndMeta => postAndMeta.Post.ID == id);    // where statement

您的键选择器不正确。它们应该接受与所讨论的表类型相同的对象,并返回在连接中使用的键。我想你的意思是:

var query = database.Posts.Join(database.Post_Metas,
                                post => post.ID,
                                meta => meta.Post_ID,
                                (post, meta) => new { Post = post, Meta = meta });

您可以随后应用where子句,而不是作为键选择器的一部分。


这件事有两种解释。使用LINQPad(如果你是LINQ新手,这是无价的)和一个虚拟数据库,我构建了以下查询:

Posts.Join(
    Post_metas,
    post => post.Post_id,
    meta => meta.Post_id,
    (post, meta) => new { Post = post, Meta = meta }
)

or

from p in Posts
join pm in Post_metas on p.Post_id equals pm.Post_id
select new { Post = p, Meta = pm }

在这种特殊情况下,我认为LINQ语法更清晰(我在两者之间进行更改,这取决于哪个更容易阅读)。

不过,我想指出的是,如果你的数据库中有适当的外键(在post和post_meta之间),那么你可能不需要显式连接,除非你试图加载大量的记录。您的示例似乎表明您正在尝试加载单个帖子及其元数据。假设每个post都有很多post_meta记录,那么你可以这样做:

var post = Posts.Single(p => p.ID == 1);
var metas = post.Post_metas.ToList();

如果您希望避免n+1问题,那么您可以显式地告诉LINQ to SQL一次性加载所有相关项(尽管对于您更熟悉L2S的人来说,这可能是一个高级主题)。下面的例子说的是“当你加载一个Post时,也通过'Post_metas'属性表示的外键加载与它相关的所有记录”:

var dataLoadOptions = new DataLoadOptions();
dataLoadOptions.LoadWith<Post>(p => p.Post_metas);

var dataContext = new MyDataContext();
dataContext.LoadOptions = dataLoadOptions;

var post = Posts.Single(p => p.ID == 1); // Post_metas loaded automagically

可以对同一类型或许多不同类型的单个DataLoadOptions集进行多个LoadWith调用。如果你经常这样做,你可能只是想考虑缓存。


可能是这样的

var myvar = from a in context.MyEntity
            join b in context.MyEntity2 on a.key equals b.key
            select new { prop1 = a.prop1, prop2= b.prop1};

因为当我开始LINQ + EntityFramework时,我盯着这些例子看了一天。

如果你正在使用EntityFramework,并且你在Post模型对象上设置了一个名为Meta的导航属性,这非常简单。如果你用的是实体,没有导航属性,还在等什么?

database
  .Posts
  .Where(post => post.ID == id)
  .Select(post => new { post, post.Meta });

如果你先做代码,你可以这样设置属性:

class Post {
  [Key]
  public int ID {get; set}
  public int MetaID { get; set; }
  public virtual Meta Meta {get; set;}
}

Daniel很好地解释了语法关系,但是为了让我的团队更容易理解,我把这个文档放在一起。希望这对大家有所帮助


这个linq查询应该为您工作。它会得到所有有post meta的帖子。

var query = database.Posts.Join(database.Post_Metas,
                                post => post.postId, // Primary Key
                                meta => meat.postId, // Foreign Key
                                (post, meta) => new { Post = post, Meta = meta });

等价SQL查询

Select * FROM Posts P
INNER JOIN Post_Metas pm ON pm.postId=p.postId

我做过类似的事情;

var certificationClass = _db.INDIVIDUALLICENSEs
    .Join(_db.INDLICENSECLAsses,
        IL => IL.LICENSE_CLASS,
        ILC => ILC.NAME,
        (IL, ILC) => new { INDIVIDUALLICENSE = IL, INDLICENSECLAsse = ILC })
    .Where(o => 
        o.INDIVIDUALLICENSE.GLOBALENTITYID == "ABC" &&
        o.INDIVIDUALLICENSE.LICENSE_TYPE == "ABC")
    .Select(t => new
        {
            value = t.PSP_INDLICENSECLAsse.ID,
            name = t.PSP_INDIVIDUALLICENSE.LICENSE_CLASS,                
        })
    .OrderBy(x => x.name);

1 = 1两个不同的表连接

var query = from post in database.Posts
            join meta in database.Post_Metas on 1 equals 1
            where post.ID == id
            select new { Post = post, Meta = meta };

LINQ连接的查询语法

var productOrderQuery = from product in Product.Setup()//outer sequence
                        join order in OrderDetails.Setup()//inner sequence
                        on product.Id equals order.ProductId //key selector
                        select new//result selector
                        {
                            OrderId = order.Id,
                            ProductId = product.Id,
                            PurchaseDate = order.PurchaseDate,
                            ProductName = product.Name,
                            ProductPrice = product.Price
                        };

LINQ连接的方法语法

var productOrderMethod = Product.Setup().//outer sequence
    Join(OrderDetails.Setup(), //inner sequence
    product => product.Id//key selector
    ,order=> order.ProductId //key selector
    ,(product,order)=> //projection result
        new
        {
            OrderId = order.Id,
            ProductId = product.Id,
            PurchaseDate = order.PurchaseDate,
            ProductName = product.Name,
            ProductPrice = product.Price
        }
    );

Product.cs供参考

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    public decimal Price { get; set; }
    public static IEnumerable<Product> Setup()
    {
        return new List<Product>()
        {
            new Product(){Id=1, Name="Bike", Price=30.33M },
            new Product(){Id=2, Name="Car", Price=50.33M },
            new Product(){Id=3, Name="Bus", Price=60.33M }
        };
    }
}

cs类,以供参考

class OrderDetails
{
    public int Id { get; set; }
    public virtual int ProductId { get; set; }

    public DateTime PurchaseDate { get; set; }
    public static IEnumerable<OrderDetails> Setup()
    {
        return new List<OrderDetails>()
        {
            new OrderDetails(){Id=1, ProductId=1, PurchaseDate= DateTime.Now },
            new OrderDetails(){Id=2, ProductId=1, PurchaseDate=DateTime.Now.AddDays(-1) },
            new OrderDetails(){Id=3, ProductId=2, PurchaseDate=DateTime.Now.AddDays(-2) }
        };
    }

}