Linq查询导致无效的列名SqlException

时间:2017-05-20 11:40:34

标签: sql asp.net-mvc linq

ASPNET MVC5网络应用程序,Visual Studio 2015

该网站是语言本地化的,因此我需要根据文化参数获取本地化的类别。

category_trans表用于提供类别名称翻译,而ISO_Languages帐户用于接收文化代码参数并将其相关ID传递给category_trans

型号:

public partial class Product
{
    public int? ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int CategoryID { get; set; }
    public virtual category_trans category_trans { get; set; }
}

public partial class Category
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int? ParentID { get; set; }
    public bool IsDeleted { get; set; }
}

public class category_trans
{
    [Key]
    [Column(Order = 1)]
    public int category_id { get; set; }
    [Key]
    [Column(Order = 2)]
    public int language_id { get; set; }
    public string name { get; set; }
}

public class ISO_Languages
{
    public int ID { get; set; }
    public string code { get; set; }
    public bool IsEnabled { get; set; }
    public string name_en { get; set; }
    public string name_fr { get; set; }
    public string name_it { get; set; }
    public string name_de { get; set; }
    public string name_es { get; set; }
}

public static IQueryable<Product> ActiveProductsPerUser(BaseContext db, string userid, string culture)
    {
        return from p in db.Products
               from ct in db.category_trans
               from l in db.ISO_Languages
               where (ct.category_id == p.CategoryID
               && ct.language_id == l.ID
               && l.code.Substring(0, 2) == culture)
               select p;
    }

导致错误:

  

System.Data.SqlClient.SqlException:列名称无效&#39; category_trans_category_id&#39;。列名称无效&#39; category_trans_language_id&#39;。在System.Data.SqlClient.SqlCommand。&lt;&gt; c.b__174_0(任务1 result)
in System.Threading.Tasks.ContinuationResultTaskFromResultTask
2.InnerInvoke()
  在System.Threading.Tasks.Task.Execute()

请注意我将预期结果传递给另一种排序和分页方法,因此接受IQueryable作为输入(无IEnumerable)。

1 个答案:

答案 0 :(得分:0)

试试这个;

public static IQueryable<Product> ActiveProductsPerUser(BaseContext db, string userid, string culture)
    {
        var query = (from p in db.Products
                     join ct in db.category_trans
                     on p.CategoryID equals ct.category_id
                     join l in db.ISO_Languages
                     on ct.language_id equals l.ID
                     where l.code.Substring(0, 2) == culture)
                     select p).ToList();

        return query;
    }