C# LINQ to SQL: Refactoring this Generic GetByID method

前端 未结 6 1657
故里飘歌
故里飘歌 2020-11-30 02:03

I wrote the following method.

public T GetByID(int id)
{
    var dbcontext = DB;
    var table = dbcontext.GetTable();
    return table.ToList().Sin         


        
6条回答
  •  情深已故
    2020-11-30 02:46

    Regarding:

    System.NotSupportedException: The member 'MusicRepo_DataContext.IHasID.ID' has no supported translation to SQL.

    The simple workaround to your initial problem is to specify an Expression. See below, it works like a charm for me.

    public interface IHasID
    {
        int ID { get; set; }
    }
    DataContext [View Code]:
    
    namespace MusicRepo_DataContext
    {
        partial class Artist : IHasID
        {
            [Column(Name = "ArtistID", Expression = "ArtistID")]
            public int ID
            {
                get { return ArtistID; }
                set { throw new System.NotImplementedException(); }
            }
        }
    }
    

提交回复
热议问题