亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用泛型和接口的 Linq 查詢

使用泛型和接口的 Linq 查詢

C#
炎炎設計 2023-09-16 16:19:51
我有幾個實現以下 IInactive 接口的實體框架類。public interface IInactive{    bool inactive { get; set; }}例如我的Order類定義如下:public class Order : IInactive{    [Key]    [Required]    public Guid orderId { get; set; }    ...    public bool inactive { get; set; }} 我正在嘗試實現一個通用方法,該方法可用于所有對象(實體),無論它們是否實現 IInactive 接口。它將被稱為如下:var query = GetAllActive<Order>();我的這個通用方法的代碼如下所示:public IQueryable<T> GetAllActive<T>() where T : class{    DbSet<T> dbSet = this._db.Set<T>();    // Does the entity implement the IInactive interface.    // If yes, only return "active" row, otherwise return all rows    if (typeof(T)is IInactive)    {        // Problem: the code in this block never executes, that is,        // (typeof(T)is IInactive) never evaluates to true       ...    }    return dbSet;}我非常感謝您幫助解決這個問題!謝謝。
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

代替

if (typeof(T) is IInactive)

嘗試

if (typeof(IInactive).IsAssignableFrom(typeof(T)))


查看完整回答
反對 回復 2023-09-16
  • 1 回答
  • 0 關注
  • 93 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號