创建不具有实体类型属性的动态 LINQ 语句

发布于 2024-08-09 08:53:51 字数 1006 浏览 7 评论 0原文

我正在尝试创建一个 Linq 语句,该语句查询实体上作为属性存在的列以及实体数据库中不作为属性存在的列。

例如,我有一个 Book 实体的集合。每本书都有一个 ID 和 Title 属性以及数据库中的匹配列。但是,假设 Book 的表还包含 Author 的列字段,而我的 Book 实体没有此字段。

涉及 ID 和标题的普通查询可能如下所示:

Books.Where(b=>b.ID == 123 && b.Title == "Title")

但是,我还想动态地将“ AND Author = 'Name' ”的等效内容添加到上述查询中。 Book 对象上不存在 Author 属性。

额外的字段及其应包含的值实际上在字典中可用。因此,我需要向查询动态添加多个 AND [FieldName] = '[Value]' 条件。

这可能有助于解释为什么我需要它来知道我正在使用 Azure 表存储,并且我已经覆盖了将 Book 实体转换为存储在 Azure 中的 XML 的序列化。

编辑 -

我尝试过使用 Dynamic Linq 库,但它不起作用。

Books.Where(b=>b.ID == 123 && b.Title == "Title").Where("Author = @0", "SomeName").ToList();

投掷: System.Linq.Dynamic.ParseException:类型“Book”中不存在属性或字段“Author”。

另外,

我知道使用 Books.AddQueryOption("$filter","Author eq 'SomeName'") 在单独使用时有效。但我不知道如何将 AddQueryOption 与现有的Where 语句一起使用。

I'm trying to create a Linq statement that queries both columns that exist on my entity as properties and those that don't but exist as columns in the database for the entity.

For example, I have a collection of Book entities. Each Book has an ID and Title property and a matching column in the database. But, lets say the table for Book also contains a column field for Author and my Book entity doesn't have this.

A normal query involving the ID and the Title might look like this:

Books.Where(b=>b.ID == 123 && b.Title == "Title")

But, I want to also dynamically add the equivalent of " AND Author = 'Name' " to the above query as well. The Author property doesn't exist on the Book object.

The extra fields and the values they should contain will actually be available in a Dictionary. So I will need to dynamically add multiple AND [FieldName] = '[Value]' conditions to the query.

It might help explain why I would need this to know that I'm using Azure Table Storage and I have overridden the serialize that turns a Book entity into the XML stored in Azure.

Edit -

I've tried using the Dynamic Linq library but it does not work.

Books.Where(b=>b.ID == 123 && b.Title == "Title").Where("Author = @0", "SomeName").ToList();

Throws:
System.Linq.Dynamic.ParseException: No property or field 'Author' exists in type 'Book'.

Also,

I know using Books.AddQueryOption("$filter","Author eq 'SomeName'") works when used by itself. But I have no idea how to use AddQueryOption along with an existing Where statement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆消瘦 2024-08-16 08:53:51

如果不将 Author 添加到 Book,您要么必须传递另一个带有 Author 的对象:

var x = new { Author="SomeName" };  
    Books.Where(b=>b.Id == 1 && b.Title == "Alpha").Where("@1.Author == @0", "SomeName", x);

或者从 Book 继承并为此目的添加一个 Author 属性:

public class BookEx : Book {
    public string Author { get; set; }
}

Books.Cast<BookEx>().Where(b=>b.Id == 1 && b.Title== "Alpha").Where("Author == @0", "SomeName");

Without adding Author to Book, you would either have to pass another object with Author on it:

var x = new { Author="SomeName" };  
    Books.Where(b=>b.Id == 1 && b.Title == "Alpha").Where("@1.Author == @0", "SomeName", x);

Or inherit from Book and add an Author property just for this purpose:

public class BookEx : Book {
    public string Author { get; set; }
}

Books.Cast<BookEx>().Where(b=>b.Id == 1 && b.Title== "Alpha").Where("Author == @0", "SomeName");
乖乖公主 2024-08-16 08:53:51

这对于 LINQ 来说是不可能的

This is not possible with LINQ

堇年纸鸢 2024-08-16 08:53:51

尝试以下代码:

var result = _tofiObjectList.Where(o => o.GetType() == mainObjType)
    .AsQueryable().Cast(relation.MainObject.EntityType).Where(relation.MainObject.Filter);

这是 IQueryable 类型的扩展方法:

public static IQueryable Cast(this IQueryable source, string type)
{
    switch (type)
    {
        case "TofiDataCollection.Service.TofiEntity.Obj":
            return source.Cast<Obj>();
    }
    return source;
}

Try the following code:

var result = _tofiObjectList.Where(o => o.GetType() == mainObjType)
    .AsQueryable().Cast(relation.MainObject.EntityType).Where(relation.MainObject.Filter);

This is an extension method for IQueryable type:

public static IQueryable Cast(this IQueryable source, string type)
{
    switch (type)
    {
        case "TofiDataCollection.Service.TofiEntity.Obj":
            return source.Cast<Obj>();
    }
    return source;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文