简体   繁体   English

将查询结果用于另一个查询

[英]Use result of a query for another query

First I have BookEntity.cs首先我有 BookEntity.cs

public class BookEntity
{
    public string StoreId { get; set; }
    public string IsbnNumber { get; set; }
    public DateTimeOffset RentStartTime { get; set; }
    public DateTimeOffset RentEndTime { get; set; }
}

Then use LINQ to query然后使用LINQ查询

var bookTable = new Table<BookEntity>(session);
var bookQuery = bookTable
                .Where(books => books.StoreId == StoreId
                             && books.IsbnNumber == IsbnNumber);

Next I have StoreEntity :接下来我有StoreEntity

public class StoreEntity
{
    public string StoreId { get; set; }
    public LocalDate Date { get; set; }
    public DateTimeOffset Timestamp { get; set; }
    public Dictionary<string, double> Readings { get; set; } = new Dictionary<string, double>();
}

What I want is to use the result from the bookQuery as the parameter to query StoreEntity table using LINQ?我想要的是使用bookQuery的结果作为参数来使用 LINQ 查询 StoreEntity 表? (use StartTime and EndTime to compare Timestamp) (使用 StartTime 和 EndTime 来比较 Timestamp)

I have seen Join from the LINQ document but it does not seem what I want because I just want to return data from Store data only not both.我已经从 LINQ 文档中看到了加入,但这似乎不是我想要的,因为我只想从存储数据返回数据,而不是两者都返回。 Thanks a lot for your help.非常感谢你的帮助。

I assume you want something like this:我假设你想要这样的东西:

var tmpList = new List<StoreEntity>;
foreach (BookEntity element in bookQuery)
{
  tmpList.Add(storeEntityTable.Where(x => x.StoreId == element.StoreId));
}

Correct me if I'm wrong.如我错了请纠正我。 If that's your case I don't know why you don't want to use a join.如果这是您的情况,我不知道您为什么不想使用联接。 You can retrieve the data you need anyways and it's way better in terms of performance.无论如何,您都可以检索所需的数据,并且在性能方面要好得多。

// //

Join query using linq:使用 linq 加入查询:

    var query =
   from myStoreEntity in storeEntityTable
   join books in bookTable on myStoreEntity.StoreId equals books.StoreId
   where myStoreEntity.StoreId == StoreId;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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