[Table]
public class Simptoms
{
[Column(IsPrimaryKey = true)]
public int ID;
[Column]
public string Name;
[Column]
public int Simptom1;
[Column]
public int Simptom2;
[Column]
public int Simptom3;
[Column]
public int Simptom4;
[Column]
public int Simptom5;
[Column]
public int Simptom6;
}
LINQ to SQL
Вопрос по LINQ к SQL:
объявление класса:
Код:
кусок кода с ошибкой:
Код:
var dataContext = new DataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MedicalNet.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
Table<Simptoms> simptoms = dataContext.GetTable<Simptoms>();
IEnumerable<Simptoms> query = (from c in simptoms
select new Simptoms
{
ID = c.ID,
Name = c.Name,
Simptom1 = c.Simptom1,
Simptom2 = c.Simptom2,
Simptom3 = c.Simptom3,
Simptom4 = c.Simptom4,
Simptom5 = c.Simptom5,
Simptom6 = c.Simptom6
}).ToList();
Вызывает: "NotSupportedException не обработано.
Явная конструкция объекта типа "diplom_01.Simptoms" в запросе не поддерживается".
Table<Simptoms> simptoms = dataContext.GetTable<Simptoms>();
IEnumerable<Simptoms> query = (from c in simptoms
select new Simptoms
{
ID = c.ID,
Name = c.Name,
Simptom1 = c.Simptom1,
Simptom2 = c.Simptom2,
Simptom3 = c.Simptom3,
Simptom4 = c.Simptom4,
Simptom5 = c.Simptom5,
Simptom6 = c.Simptom6
}).ToList();
Вызывает: "NotSupportedException не обработано.
Явная конструкция объекта типа "diplom_01.Simptoms" в запросе не поддерживается".
Хотелось бы разобраться в чем проблема. Заранее спасибо.
Решение 1: используем анонимный тип в проекции:
Код:
var query = (from c in simptoms
select new
{
ID = c.ID,
Name = c.Name,
Simptom1 = c.Simptom1,
Simptom2 = c.Simptom2,
Simptom3 = c.Simptom3,
Simptom4 = c.Simptom4,
Simptom5 = c.Simptom5,
Simptom6 = c.Simptom6
})
select new
{
ID = c.ID,
Name = c.Name,
Simptom1 = c.Simptom1,
Simptom2 = c.Simptom2,
Simptom3 = c.Simptom3,
Simptom4 = c.Simptom4,
Simptom5 = c.Simptom5,
Simptom6 = c.Simptom6
})
Я делаю так:
Код:
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.cuGoods")]
public partial class cuGoodsItem : INotifyPropertyChanging, INotifyPropertyChanged
{
//...
}
public partial class cuGoodsItem_Projection_PriceList : cuGoodsItem
{
private bool _Selected;
//...
}
public partial class cuGoodsItem : INotifyPropertyChanging, INotifyPropertyChanged
{
//...
}
public partial class cuGoodsItem_Projection_PriceList : cuGoodsItem
{
private bool _Selected;
//...
}
Код:
var qq1 = from goods in _dc.cuGoodsItems.Where(item => !item.IsDeleted
&& item.ParentID == parentID
&& item.Name != "..")
join selected in
_dc.MX_filters.Where(filter => filter.UnitID == UnitID && filter.FilterID == FilterTypeID)
on goods.ID equals selected.Value into sv
from subval in sv.DefaultIfEmpty()
let projectionItem = new cuGoodsItem_Projection_PriceList
{
ID = goods.ID,
Type = goods.Type,
Group = goods.Group,
Order = goods.Order,
ParentID = goods.ParentID,
Name = goods.Name,
PathName = goods.PathName,
Nomnum = goods.Nomnum,
PriceC = goods.PriceC,
Selected = subval.Value != null
}
select projectionItem;
&& item.ParentID == parentID
&& item.Name != "..")
join selected in
_dc.MX_filters.Where(filter => filter.UnitID == UnitID && filter.FilterID == FilterTypeID)
on goods.ID equals selected.Value into sv
from subval in sv.DefaultIfEmpty()
let projectionItem = new cuGoodsItem_Projection_PriceList
{
ID = goods.ID,
Type = goods.Type,
Group = goods.Group,
Order = goods.Order,
ParentID = goods.ParentID,
Name = goods.Name,
PathName = goods.PathName,
Nomnum = goods.Nomnum,
PriceC = goods.PriceC,
Selected = subval.Value != null
}
select projectionItem;