我已经阅读了其他一些帖子,但他们没有帮助。
CarPart是EF4基因类
[EdmEntityTypeAttribute(NamespaceName="xxxx.Data.Domain.Model", Name="CarPart")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class CarPart : EntityObject
{
#region Factory Method
/// summary>
/// Create a new CarPart object.
/// </summary>
/// <param name="carPartId">Initial value of the CarPartId property.</param>
/// <param name="name">Initial value of the Name property.</param>
/// <param name="carPartTypeId">Initial value of the CarPartId property.</param>
public static CarPart CreateCarPart(global::System.Int32 carPartId, global::System.String name, global::System.Int32 carPartId)
{
CarPart carPart = new CarPart();
carPart.CarPartId = carPartId;
carPart.Name = name;
carPart.CarPartTypeId = carPartTypeId;
return carPart;
}
#endregion
#region Primitive Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 CarPartId
{
get
{
return _CarPartId;
}
set
{
if (_CarPartId != value)
{
OnCarPartIdChanging(value);
ReportPropertyChanging("CarPartId");
_CarPartId = StructuralObject.SetValidValue(value);
ReportPropertyChanged("CarPartIdId");
OnCarPartIdChanged();
}
}
}
private global::System.Int32 _CarPartId;
partial void OnCarPartIdChanging(global::System.Int32 value);
partial void OnCarPartIdChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String Name
{
get
{
return _Name;
}
set
{
OnNameChanging(value);
ReportPropertyChanging("Name");
_Name = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("Name");
OnNameChanged();
}
}
private global::System.String _Name;
partial void OnNameChanging(global::System.String value);
partial void OnNameChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 CarPartTypeId
{
get
{
return _CarPartTypeId;
}
set
{
OnCarPartTypeIdChanging(value);
ReportPropertyChanging("CarPartTypeId");
_CarPartTypeId = StructuralObject.SetValidValue(value);
ReportPropertyChanged("CarPartTypeId");
OnCarPartTypeIdChanged();
}
}
private global::System.Int32 _CarPartTypeId;
partial void OnCarPartTypeIdChanging(global::System.Int32 value);
partial void OnCarPartTypeIdChanged();
#endregion
#region Navigation Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("xxxx.Data.Domain.Model", "FK_CarPartId", "Part")]
public EntityCollection<Part> Parts
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Part>("xxxxx.Data.Domain.Model.FK_CarPartId", "Part");
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Part>("xxxx.Data.Domain.Model.FK_CarPartId", "Part", value);
}
}
}
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("xxxxx.Data.Domain.Model", "FK_CarPartTypeId", "CarPartType")]
public CarPartType CarPartType
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<CarPartType>("xxxx.Data.Domain.Model.FK_CarPartTypeId", "CarPartType").Value;
}
set
{
((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<CarPartType>("xxxxx.Data.Domain.Model.FK_CarPartTypeId", "CarPartType").Value = value;
}
}
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[BrowsableAttribute(false)]
[DataMemberAttribute()]
public EntityReference<CarPartType> CarPartTypeReference
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<CarPartType>("xxxx.Data.Domain.Model.FK_CarPartTypeId", "CarPartType");
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<CarPartType>("xxxxxx.Data.Domain.Model.FK_CarPartTypeId", "CarPartType", value);
}
}
}
#endregion
}
所以这是我的加入代码:
List<Parts> parts = _context.Parts.Where(p => p.PartId == partId).ToList();
List<CarParts> parts = _context.CarParts
.Join(parts, cp => cp.PartId, p => p.PartId, (cp, p) => cp).ToList();
错误:无法创建“Model.CarParts”类型的常量值。只要 支持原始类型(例如Int32,String和Guid') 这个背景。
试图看着它但却无法超越这个。我对LINQ-To-SQL来说有点新......虽然数量很多但不是很多(主要是LINQ to Objects),所以很新加入这个。
答案 0 :(得分:0)
如果我理解正确你想做这样的事情:
internal class Db
{
public Db()
{
var parts = new List<Part>
{
new Part() {PartId = 1, PartName = "Part 1"},
new Part() {PartId = 2, PartName = "Part 2"},
};
Parts = parts.AsQueryable();
var carParts = new List<CarPart>
{
new CarPart() {CarPartId = 1, CarPartName = "Car Part 1.1", PartId = 1},
new CarPart() {CarPartId = 1, CarPartName = "Car Part 1.2", PartId = 1},
new CarPart() {CarPartId = 1, CarPartName = "Car Part 2.1", PartId = 2},
};
CarParts = carParts.AsQueryable();
}
public IQueryable<Part> Parts { get; set; }
public IQueryable<CarPart> CarParts { get; set; }
}
internal class CarPart
{
public int CarPartId { get; set; }
public string CarPartName { get; set; }
public int PartId { get; set; }
}
internal class Part
{
public int PartId { get; set; }
public string PartName { get; set; }
}
static void Main(string[] args)
{
Db db = new Db();
var result = from carPart in db.CarParts
join part in db.Parts on carPart.PartId equals part.PartId
select new {Part = part, CarPart = carPart};
var lambdaResult = db.CarParts.Join(db.Parts, part => part.PartId, caPart => caPart.PartId,
(carPart, part) => new {CarPart = carPart, Part = part});
foreach (var item in result)
{
Console.WriteLine(item.Part.PartName);
Console.WriteLine(item.CarPart.CarPartName);
}
Console.WriteLine("------------");
foreach (var item in lambdaResult)
{
Console.WriteLine(item.Part.PartName);
Console.WriteLine(item.CarPart.CarPartName);
}
}
结果是一个新的匿名对象,其中连接的对象作为内容。 它会将其打印到控制台:
Part 1
Car Part 1.1
Part 1
Car Part 1.2
Part 2
Car Part 2.1
-------
Part 1
Car Part 1.1
Part 1
Car Part 1.2
Part 2
Car Part 2.1