Linq to SQL中报错"不允许在查询中显式构造实体类型"怎么解决

2024-11-20 22:37:31
推荐回答(2个)
回答1:

public IQueryable GetxxList()
{
var query = from t in a1
from tp in t.a2
group t by t.Name into g
select new a1{ Name = g.Key, cc= g.Count() }
return query;
}
注意:下面有两种情况,第一种会报“不允许在查询中显式构造实体类型”错误
第一种:
[Table(Name="表名")]
public class a1{
[Column(IsPrimaryKey = true,CanBeNull=false,IsDbGenerated=true,AutoSync=AutoSync.OnInsert)]
public int cc{get;set;}
[Column(Name = "Name")]
public string Name{get;set;}
}
第二种:正确的
public class a1{
public int cc{get;set;}
public string Name{get;set;}
}

回答2:

代码贴上来