久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C#如何轉(zhuǎn)換一個表達式<Func<SomeType&

C# How to convert an Expressionlt;Funclt;SomeTypegt;gt; to an Expressionlt;Funclt;OtherTypegt;gt;(C#如何轉(zhuǎn)換一個表達式lt;Funclt;SomeTypegt;gt;到表達式lt;Funclt;OtherTypegt;gt;)
本文介紹了C#如何轉(zhuǎn)換一個表達式<Func<SomeType>>到表達式<Func<OtherType>>的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我之前使用過基于 lamdas 的 C# 表達式,但我沒有手工編寫它們的經(jīng)驗.給定一個 Expression>originalPredicate,我想創(chuàng)建一個Expression>翻譯謂詞.

I have used C# expressions before based on lamdas, but I have no experience composing them by hand. Given an Expression<Func<SomeType, bool>> originalPredicate, I want to create an Expression<Func<OtherType, bool>> translatedPredicate.

在這種情況下 SomeType 和 OtherType 具有相同的字段,但它們不相關(guān)(沒有繼承,也不是基于公共接口).

In this case SomeType and OtherType have the same fields, but they are not related (no inheritance and not based on a common interface).

背景:我有一個基于 LINQ to SQL 的存儲庫實現(xiàn).我將 LINQ to SQL 實體投影到我的模型實體,以將我的模型保存在 POCO 中.我想將表達式傳遞給存儲庫(作為規(guī)范的一種形式),但它們應該基于模型實體.但我無法將這些表達式傳遞給數(shù)據(jù)上下文,因為它需要基于 LINQ to SQL 實體的表達式.

Background: I have a repository implementation based on LINQ to SQL. I project the LINQ to SQL entities to my Model entities, to keep my model in POCO. I want to pass expressions to the repository (as a form of specifications) but they should be based on the model entities. But I can't pass those expressions to the data context, since it expects expressions based on the LINQ to SQL entities.

推薦答案

Expression,最簡單的方法就是用一個轉(zhuǎn)換表達式:

With Expression, the simplest way is with a conversion expression:

class Foo {
    public int Value { get; set; }
}
class Bar {
    public int Value { get; set; }
}
static class Program {
    static void Main() {
        Expression<Func<Foo, bool>> predicate =
            x => x.Value % 2 == 0;
        Expression<Func<Bar, Foo>> convert =
            bar => new Foo { Value = bar.Value };

        var param = Expression.Parameter(typeof(Bar), "bar");
        var body = Expression.Invoke(predicate,
              Expression.Invoke(convert, param));
        var lambda = Expression.Lambda<Func<Bar, bool>>(body, param);

        // test with LINQ-to-Objects for simplicity
        var func = lambda.Compile();
        bool withOdd = func(new Bar { Value = 7 }),
             withEven = func(new Bar { Value = 12 });
    }
}

但是請注意,不同的提供商會對此提供不同的支持.例如,EF 可能不喜歡它,即使 LINQ-to-SQL 喜歡.

Note however that this will be supported differently by different providers. EF might not like it, for example, even if LINQ-to-SQL does.

另一種選擇是完全重建表達式樹,使用反射找到對應的成員.復雜得多.

The other option is to rebuild the expression tree completely, using reflection to find the corresponding members. Much more complex.

這篇關(guān)于C#如何轉(zhuǎn)換一個表達式&lt;Func&lt;SomeType&gt;&gt;到表達式&lt;Func&lt;OtherType&gt;&gt;的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設(shè)置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調(diào)用)
主站蜘蛛池模板: 国产高清免费 | 男人天堂久久久 | 国产在线精品一区二区三区 | 亚欧性视频 | 国产又色又爽又黄又免费 | 99久久亚洲| 91日韩在线 | 成人在线一区二区三区 | 久久这里有精品 | 国产电影一区二区 | 久久久www成人免费无遮挡大片 | 久久久久久国产精品久久 | 亚洲不卡在线观看 | 一级黄色播放 | 国产成人精品一区二区三区在线 | 丁香六月激情 | 日韩精品久久久 | 日日日操| 日韩一区二区三区精品 | 国产成人精品午夜视频免费 | 日韩国产在线 | 欧美精品一区二区在线观看 | 国产1区2区3区 | 中文字幕 国产 | 国产精品综合一区二区 | 日韩国产精品一区二区三区 | 91色视频在线观看 | 五月网婷婷 | 亚洲第一福利视频 | 97久久超碰 | 免费99精品国产自在在线 | 毛片av免费在线观看 | 中文字幕日韩欧美一区二区三区 | 亚洲成人综合在线 | 免费观看黄| 久久精品亚洲欧美日韩精品中文字幕 | 一级片网站视频 | 四虎午夜剧场 | 欧美一区二区三区一在线观看 | 亚洲高清一区二区三区 | 久久精品国产一区二区电影 |