序列中包含一个以上的元素 [英] Sequence contains more than one element
问题描述
我在通过LINQ的霎那型RhsTruck的名单,让他们展示一些问题。
RhsTruck只是有性质在品牌,型号,序列等... RhsCustomer具有属性客户名称,CustomerAddress,等等...
我不断收到错误序列包含多个元素。有任何想法吗?难道我处理这个错误的方式?
公共RhsCustomer GetCustomer(字符串customerNumber之)
{
使用(RhsEbsDataContext上下文=新RhsEbsDataContext())
{
RhsCustomer RC =(从x在context.custmasts
其中,x.kcustnum == customerNumber之
选择新RhsCustomer()
{
客户名称= x.custname,
CustomerAddress = x.custadd +,+ x.custcity
CustomerPhone = x.custphone,
CustomerFax = x.custfax
})的SingleOrDefault()。
返回RC;
}
}
公开名单< RhsTruck> GetEquipmentOwned(RhsCustomer卡斯特)
{
使用(RhsEbsDataContext上下文=新RhsEbsDataContext())
{
VAR卡车=(从米context.mkpops
加入c在context.custmasts
在m.kcustnum等于c.kcustnum
其中,m.kcustnum == cust.CustomerNumber
选择新RhsTruck
{
使= m.kmfg,
模型= m.kmodel,
串行= m.kserialnum,
EquipID = m.kserialno1,
IsRental = FALSE
})了ToList()。
返回卡车;
}
}
保护无效的Page_Load(对象发件人,EventArgs的)
{
。字符串testCustNum = Page.Request.QueryString [custnum]的ToString();
RhsCustomerRepository rcrep =新RhsCustomerRepository();
RhsCustomer RC = rcrep.GetCustomer(testCustNum);
名单< RhsTruck>卡车= rcrep.GetEquipmentOwned(RC);
//我要显示的列表到一个GridView W /自动生成列
GridViewTrucks.DataSource =卡车;
GridViewTrucks.DataBind();
}
现在的问题是,你正在使用<一个href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.singleordefault.aspx"><$c$c>SingleOrDefault$c$c>.当集合正好包含0或1元这个方法才能成功。我相信你正在寻找<一href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.firstordefault.aspx"><$c$c>FirstOrDefault$c$c>这将不管有多少元素集合中取得成功。
I'm having some issues with grabbing a list of type "RhsTruck" through Linq and getting them to display.
RhsTruck just has properites Make, Model, Serial etc... RhsCustomer has properties CustomerName, CustomerAddress, etc...
I keep getting the error "Sequence contains more than one element". Any ideas? Am I approaching this the wrong way?
public RhsCustomer GetCustomer(string customerNumber)
{
using (RhsEbsDataContext context = new RhsEbsDataContext() )
{
RhsCustomer rc = (from x in context.custmasts
where x.kcustnum == customerNumber
select new RhsCustomer()
{
CustomerName = x.custname,
CustomerAddress = x.custadd + ", " + x.custcity
CustomerPhone = x.custphone,
CustomerFax = x.custfax
}).SingleOrDefault();
return rc;
}
}
public List<RhsTruck> GetEquipmentOwned(RhsCustomer cust)
{
using (RhsEbsDataContext context = new RhsEbsDataContext())
{
var trucks = (from m in context.mkpops
join c in context.custmasts
on m.kcustnum equals c.kcustnum
where m.kcustnum == cust.CustomerNumber
select new RhsTruck
{
Make = m.kmfg,
Model = m.kmodel,
Serial = m.kserialnum,
EquipID = m.kserialno1,
IsRental = false
}).ToList();
return trucks;
}
}
protected void Page_Load(object sender, EventArgs e)
{
string testCustNum = Page.Request.QueryString["custnum"].ToString();
RhsCustomerRepository rcrep = new RhsCustomerRepository();
RhsCustomer rc = rcrep.GetCustomer(testCustNum);
List<RhsTruck> trucks = rcrep.GetEquipmentOwned(rc);
// I want to display the List into a Gridview w/auto-generated columns
GridViewTrucks.DataSource = trucks;
GridViewTrucks.DataBind();
}
The problem is that you are using SingleOrDefault
. This method will only succeed when the collections contains exactly 0 or 1 element. I believe you are looking for FirstOrDefault
which will succeed no matter how many elements are in the collection.
这篇关于序列中包含一个以上的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!