How to return all records from a table

Asked

Viewed 640 times

1

In my context I have a File table, and another version of the file, so I did with Entity to return a file, and only one version of the file:

 internal Arquivo GetArquivo(string termo)
    {
        using (var ctx = new GEDEntities())
        {
            var arquivo = (from arq in ctx.ARQUIVO
                           where arq.ARQUIVO_GUID == termo
                           || arq.XARQUIVO == termo
                           select new Arquivo()
                           {
                               ARQUIVO_GUID = arq.ARQUIVO_GUID,
                               DIRETORIO_GUID = arq.DIRETORIO_GUID,
                               EXTENSAO = arq.EXTENSAO,
                               IS_STREAM = arq.IS_STREAM,
                               TAG = arq.TAG,
                               TIPO_DE_ARQUIVO_GUID = arq.TIPO_DE_ARQUIVO_GUID,
                               ULT_ARQUIVO_VERSAO_GUID = arq.ULT_ARQUIVO_VERSAO_GUID,
                               URL = arq.URL,
                               XARQUIVO = arq.XARQUIVO,
                           }).FirstOrDefault();
            if (arquivo == null)
                throw new ArquivoException("Arquivo não encontrado");
            arquivo.ArquivoVersoes.Add(GetArquivoVersao(arquivo.ULT_ARQUIVO_VERSAO_GUID));
            return arquivo;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="arquivoVersaoGuid"></param>
    /// <returns></returns>
    internal ArquivoVersao GetArquivoVersao(string arquivoVersaoGuid)
    {
        using (var ctx = new GEDEntities())
        {
            var versao = (from ver in ctx.ARQUIVO_VERSAO
                          where ver.ARQUIVO_VERSAO_GUID == arquivoVersaoGuid
                          select new ArquivoVersao()
                          {
                              ARQUIVO_GUID = ver.ARQUIVO_GUID,
                              ARQUIVO = ver.ARQUIVO,
                              USUARIO_PESSOA_GUID = ver.USUARIO_PESSOA_GUID,
                              TAMANHO = ver.TAMANHO,
                              DATAHORA = ver.DATAHORA,
                              ARQUIVO_VERSAO_GUID = ver.ARQUIVO_VERSAO_GUID
                          }).FirstOrDefault();
            if (versao == null)
                throw new ArquivoException("Versão não encontrada");
            return versao;
        }

File class

public class Arquivo
{
    public Arquivo() 
    {
        ArquivoVersoes = new List<ArquivoVersao>();
    }
    public string ARQUIVO_GUID { get; set; }
    public string XARQUIVO { get; set; }
    public string TAG { get; set; }
    public string EXTENSAO { get; set; }
    public string URL { get; set; }
    public bool IS_STREAM { get; set; }
    public string ULT_ARQUIVO_VERSAO_GUID { get; set; }
    public string TIPO_DE_ARQUIVO_GUID { get; set; }
    public string DIRETORIO_GUID { get; set; }
    public TipoDeArquivo TipoDeArquivo { get; set; }
    public List<ArquivoVersao> ArquivoVersoes { get; set; } 

Class Arquivoversao

 public class ArquivoVersao
{    

    public string ARQUIVO_VERSAO_GUID { get; set; }
    public string ARQUIVO_GUID { get; set; }
    public byte[] ARQUIVO { get; set; }
    public string USUARIO_PESSOA_GUID { get; set; }
    public int TAMANHO { get; set; }
    public System.DateTime DATAHORA { get; set; }
}

How would a way to receive all the verses from the aqruivos?

  • 1

    You have been here a long time. Try to put titles that help identify the problem. This title means nothing. Describe the problem briefly in a short sentence. No need to say in the title that you are using EF, the tag says so already.

  • but I’m not sure how to specify that context

  • if you can help me I really appreciate.

  • improved, is not ideal, but goes practicing. This helps you even think better about the problem, or who knows describe it better to receive better help.

  • in this case, it is possible to have only one method, which returns the file and all its versions.

2 answers

1

I believe that’s exactly how Tobymosque responded, just complementing:

Why not use Firstordefault() in this case?

Firstordefault() returns the first element of a sequence or a default value if no element is found.

So, since you need to return a list with all versions and not just one, you can use Métod Tolist(), that will return a list of versions.

Why change the method signature?

You need to change the signature of your method so that it returns a collection (as a list, see in the example below), the way it is in the question will return only one version:

internal List<ArquivoVersao> GetArquivoVersao(string arquivoVersaoGuid)

valeu, explains this line as well: explains this line pf: from Arq in ctx.FILE Join ver in ctx.ARQUIVO_VERSAO on Arq.ARQUIVO_GUID equals ver.ARQUIVO_GUID

LINQ Join, as well as SQL language Join, joins elements from two collections based on some condition.

In the case of this query, it will be a junction of elements of the FILE table with elements of the ARQUIVO_VERSAO table where the ARQUIVO_GUID field of the FILE table is equal to the ARQUIVO_GUID field of the ARQUIVO_VERSAO table.

  • and why (Guid arquivoGuid) ?? what is this guid?

  • Was typing error :-) already adjusted.

  • Thanks, explain this line too: explains this line pf: from Arq in ctx.FILE Join ver in ctx.ARQUIVO_VERSAO on Arq.ARQUIVO_GUID equals ver.ARQUIVO_GUID

  • Guid is a data structure, in other languages it is known as UUID, basically it is 16 bytes that can be represented by a string in the following format: 00112233-4455-6677-8899-AABBCCDDEEFF

  • from Arq in ctx.FILE Join ver in ctx.ARQUIVO_VERSAO on Arq.ARQUIVO_GUID equals ver.ARQUIVO_GUIID, is only a union between FILE and ARQUIVO_VERSAO files. For the above example it really is unnecessary. from ver in ctx.ARQUIVO_VERSAO would be enough.

  • but the error in the following line : Where Arq.ARQUIVO_VERSAO == fileVersaoGuid says it cannot compare with string

  • can update your question by placing the structure of the File and File classes?

  • updated the question

  • Strange, I didn’t see in the code of your question that line you said is giving error.

  • go to chat now,

Show 6 more comments

0


You can try the following:

internal ArquivoVersao GetArquivoVersao(string arquivoGuid)
{
    using (var ctx = new GEDEntities())
    {
        var versoes = (
                from ver in ctx.ARQUIVO_VERSAO
                where ver.ARQUIVO_GUID == arquivoGuid
                select new ArquivoVersao()
                {
                    ARQUIVO_GUID = ver.ARQUIVO_GUID,
                    ARQUIVO = ver.ARQUIVO,
                    USUARIO_PESSOA_GUID = ver.USUARIO_PESSOA_GUID,
                    TAMANHO = ver.TAMANHO,
                    DATAHORA = ver.DATAHORA,
                    ARQUIVO_VERSAO_GUID = ver.ARQUIVO_VERSAO_GUID
                }
            ).ToList();
        if (versoes == null)
            throw new ArquivoException("Versão não encontrada");
        return versoes;
    }
}

Another option is to change the Getfile method and remove Getarquivoversao:

internal Arquivo GetArquivo(string termo)
{
    using (var ctx = new GEDEntities())
    {
        var arquivo = (
            from arq in ctx.ARQUIVO
            join ver in ctx.ARQUIVO_VERSAO on arq.ARQUIVO_GUID equals ver.ARQUIVO_GUID
            where arq.ARQUIVO_GUID == termo || arq.XARQUIVO == termo
            group ver by new {
                ARQUIVO_GUID = arq.ARQUIVO_GUID,
                DIRETORIO_GUID = arq.DIRETORIO_GUID,
                EXTENSAO = arq.EXTENSAO,
                IS_STREAM = arq.IS_STREAM,
                TAG = arq.TAG,
                TIPO_DE_ARQUIVO_GUID = arq.TIPO_DE_ARQUIVO_GUID,
                ULT_ARQUIVO_VERSAO_GUID = arq.ULT_ARQUIVO_VERSAO_GUID,
                URL = arq.URL,
                XARQUIVO = arq.XARQUIVO,
            } into arqVersoes
            select new Arquivo()
            {
                ARQUIVO_GUID = arqVersoes.Key.ARQUIVO_GUID,
                DIRETORIO_GUID = arqVersoes.Key.DIRETORIO_GUID,
                EXTENSAO = arqVersoes.Key.EXTENSAO,
                IS_STREAM = arqVersoes.Key.IS_STREAM,
                TAG = arqVersoes.Key.TAG,
                TIPO_DE_ARQUIVO_GUID = arqVersoes.Key.TIPO_DE_ARQUIVO_GUID,
                ULT_ARQUIVO_VERSAO_GUID = arqVersoes.Key.ULT_ARQUIVO_VERSAO_GUID,
                URL = arqVersoes.Key.URL,
                XARQUIVO = arqVersoes.Key.XARQUIVO,
                ArquivoVersoes = arquivo.toList()
            }).FirstOrDefault();
        if (arquivo == null)
            throw new ArquivoException("Arquivo não encontrado");
        return arquivo;
    }
}

PS: Code not tested.

  • explains this line pf: from Arq in ctx.FILE Join ver in ctx.ARQUIVO_VERSAO on Arq.ARQUIVO_GUID equals ver.ARQUIVO_GUID equals ver.

  • I made some changes based on the recommendations of Mr @Renan

  • still of the comparison error but of the error in the following line : Where Arq.ARQUIVO_VERSAO == fileVersaoGuid says it cannot compare with string

  • worked, worked

  • ops still with error, I will post the file class, and arquivoversao

  • changed the question

  • is just a method? and how do I know if I want 1 version or all?

  • I prefer a separate method, but how does it end up? keeps giving error in comparison ==

  • Friend went all right. Thanks

  • guy has got to help me here

  • chat here: http://chat.stackexchange.com/rooms/20863/discussion-between-war-lock-and-tobymosque

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.