C# How i use linq with explicit interface attribute?

249 views Asked by At

i have one class with two inheritance interfaces and yours attributes are explicits because both have some equals attributes, so, i need use LINQ with this class, but i can't access the explicits attributes when i use "select new Foo" ... look the case:

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; }

....

and my 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();
3

There are 3 answers

0
Marcelo Rodrigues On BEST ANSWER

i found one way, i just create one proxy to use the attributes of the interfaces in my class, look:

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; }

... }

this solved my problem!!! i hope it helps all...

0
Tim S. On

If possible, implement the interfaces implicitly instead of explicitly, as Servy suggested.

If that won't work for you, use method syntax for this portion instead of query syntax, so that you can include a multi-line delegate instead of a single-expression one. This lets you cast to access the hidden properties.

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();
2
bjaksic On

You could just add a backing field for the explicit interface implementation. This way you are implementing an interface and are able to get/set values.

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;
        }
    }
}