C# 我如何使用具有显式接口属性的 linq?

C# How i use linq with explicit interface attribute?

我有一个 class 有两个继承接口,你的属性是显式的,因为它们都有一些 equals 属性,所以,我需要对这个 class 使用 LINQ,但我无法访问当我使用 "select new Foo" 时显式属性 ... 看情况:

public class QuestaoMontaProva : IQuestao, IExercicio
{

    public int Discordo { get; set; }
    public int Rever { get; set; }
    public int Anotacao { get; set; }
    public int Realizada { set; get; }
    public int Ativo { set; get; }


    int IQuestao.Id { get; set; }

    string IQuestao.EnunciadoQuestao { get; set; }

    string IQuestao.ExercicioTipo { get; set; }

.....

和我的 LINQ:

var flags = (from b in dt.AsEnumerable()

                             select new QuestaoMontaProva
                             {
                                 IdQuestao = Convert.ToInt32(b["ID_QUESTAO"]), // i can't access this
                                 IdTipoExercicio = Convert.ToInt32(b["ID_TIPOEXERCICIO"]),// i can't access this
                                 Discordo = Convert.ToInt32(b["DISCORDO"]),
                                 Rever = Convert.ToInt32(b["REVER"]),
                                 Anotacao = Convert.ToInt32(b["ANOTACAO"]),
                                 Realizada = Convert.ToInt32(b["REALIZADA"]),
                                 Correta = Convert.ToInt32(b["CORRETA"]),
                                 Ativo = Convert.ToInt32(b["ATIVO"])
                             }).ToList();

如果可能,隐式而不是显式地实现接口,如

如果这对您不起作用,请对这部分使用方法语法而不是查询语法,这样您就可以包含多行委托而不是单表达式委托。这使您可以进行强制转换以访问隐藏属性。

var flags = dt.AsEnumerable().Select(b =>
{
    var q = new QuestaoMontaProva
    {
        Discordo = Convert.ToInt32(b["DISCORDO"]),
        Rever = Convert.ToInt32(b["REVER"]),
        Anotacao = Convert.ToInt32(b["ANOTACAO"]),
        Realizada = Convert.ToInt32(b["REALIZADA"]),
        Correta = Convert.ToInt32(b["CORRETA"]),
        Ativo = Convert.ToInt32(b["ATIVO"])
    };
    var iq = (IQuestao)q;
    iq.Id = Convert.ToInt32(b["ID_QUESTAO"]);
    iq.ExercicioTipo = Convert.ToInt32(b["ID_TIPOEXERCICIO"]);
    return q;
}).ToList();

您可以只为显式接口实现添加一个支持字段。这样你就实现了一个接口并且能够 get/set 值。

var query = from i in items
            select new QuestaoMontaProva
            {
                Id = 1,
                IQuestaoId = 2,
                IExercicioId = 2
            };

public interface IQuestao
{
    int Id { get; set; }
}

public interface IExercicio
{
    int Id { get; set; }
}

public class QuestaoMontaProva : IQuestao, IExercicio
{
    public int Id { get; set; }
    public int IQuestaoId { get; set; }
    public int IQuestao.Id
    {
        get
        {
            return IQuestaoId;
        }
        set
        {
            IQuestaoId = value;
        }
    }

    public int IExercicioId { get; set; }
    public int IExercicio.Id
    {
        get
        {
            return IExercicioId;
        }
        set
        {
            IExercicioId = value;
        }
    }
}

我找到了一种方法,我只是创建一个代理来使用我 class 中接口的属性,看:

public class QuestaoMontaProva : IQuestao, IExercicio {

    public int Discordo { get; set; }
    public int Rever { get; set; }
    public int Anotacao { get; set; }
    public int Realizada { set; get; }
    public int Ativo { set; get; }
    public int IdEspecialidade { get; set; }
    public string NomeEspecialidade { set; get; }
    public string DescricaoAlternativa { set; get; }
    public int IdQuestao { get { return (this as IQuestao).Id; } set { (this as IQuestao).Id = value; } }
    public int IdTipoExercicio { get { return (this as IQuestao).IdTipoExercicio; } set { (this as IQuestao).IdTipoExercicio = value; } }
    public int Correta { get { return (this as IQuestao).Correta; } set { (this as IQuestao).Correta = value; } }
    public int Ano { get { return (this as IExercicio).Ano; } set { (this as IExercicio).Ano = value; } }
    public int IdExercicio { get { return (this as IExercicio).Id; } set { (this as IExercicio).Id = value; } }
    public string NomeExercicio { get { return (this as IExercicio).Nome; } set { (this as IExercicio).Nome = value; } }
    public string Regiao { get { return (this as IExercicio).txtRegiao; } set { (this as IExercicio).txtRegiao = value; } }
    public string EnunciadoQuestao { get { return (this as IQuestao).EnunciadoQuestao; } set { (this as IQuestao).EnunciadoQuestao = value; } }
    public string GuidQuestao { get { return (this as IQuestao).GuidQuestao; } set { (this as IQuestao).GuidQuestao = value; } }


    public int IQuestao.Id { get; set; }

    string IQuestao.EnunciadoQuestao { get; set; }

    string IQuestao.ExercicioTipo { get; set; }

    List<Especialidade> IQuestao.Especialidades { get; set; }

... }

这解决了我的问题!!! 希望对大家有所帮助...