简体   繁体   English

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

[英]C# How i use linq with explicit interface attribute?

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:我有一个带有两个继承接口的类,你的属性是显式的,因为它们都有一些相等的属性,所以,我需要在这个类中使用 LINQ,但是当我使用“选择新的 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; }

.... ....

and my LINQ :和我的 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();

If possible, implement the interfaces implicitly instead of explicitly, as Servy suggested .如果可能的话,像Servy 建议的那样,隐式地而不是显式地实现接口。

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();

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

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 {公开课 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...我希望它对大家有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM