我有三个模型和一个View模型。该模型是这样的:生成序列号。在列表中使用LINQ
public partial class SalesDetail
{
public long IID { get; set; }
public Nullable<long> SalesId { get; set; }
public int ItemId { get; set; }
public int UoM { get; set; }
public int Qty { get; set; }
public Nullable<decimal> Disc { get; set; }
public virtual General General { get; set; }
public virtual Item Item { get; set; }
public virtual Sale Sale { get; set; }
}
public partial class Item
{
public Item()
{
this.SalesDetails = new HashSet<SalesDetail>();
}
public int IID { get; set; }
public string Item1 { get; set; }
public int UoM { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<SalesDetail> SalesDetails { get; set; }
}
public partial class General
{
public General()
{
this.Parties = new HashSet<Party>();
this.Parties1 = new HashSet<Party>();
this.SalesDetails = new HashSet<SalesDetail>();
}
public int IID { get; set; }
public string Value { get; set; }
public string Desciption { get; set; }
public byte Type { get; set; }
public Nullable<int> ParentID { get; set; }
}
我也有一个像视图模型:
public class SalelsDetailListViewModel
{
public int SerialNo { get; set; }
public Int64 Id { get; set; }
public int ItemId { get; set; }
public string ItemName { get; set; }
public int UoM { get; set; }
public string UnitName { get; set; }
public int Qty { get; set; }
public decimal? Disc { get; set; }
}
我试图生成序列号。同时执行linq查询作为列表。稍后,此列表将显示为序列号为的表格。 我写了下面的查询。
lstDetails = (from d in db.SalesDetails
join i in db.Items on d.ItemId equals i.IID
join u in db.Generals on d.UoM equals u.IID
select new SalelsDetailListViewModel
{
SerialNo = ???,
Id = d.IID,
ItemId = i.IID,
ItemName = i.Item1,
UoM = u.IID,
UnitName = u.Value,
Qty = d.Qty,
Disc = d.Disc
}).ToList();
我不明白如何增加与其他项目的查询内序列号。 任何帮助将被感谢接受。
感谢
帕塔
就在你声明'lstDetails'之前,你可以声明'int count = 0'并且执行'SerialNo = count ++' 但是如果你在运行中生成它,你将如何使用那个串行?对于你的客户来说,这是没有意义的,如果有人在视图中看到它并将其发回,你将如何将它与你的数据相匹配? – ironstone13
我已经尝试过,但序列号总是0. – Partha
我已经测试了我的建议,它的工作原理。我在下面的答案中提供了一些替代方案。让我知道,如果它不适合你,我们会找出原因。 – ironstone13