Bum*_*Bee 3 ienumerable xelement
我的查询的返回类型是IEnumerable<XElement>.如何将结果数据转换为XElement类型?可能吗?有些人可以帮助我理解这一点.
var resQ = from e in docElmnt.Descendants(xmlns + Constants.T_ROOT)
.Where(x => x.Attribute(Constants.T_ID).Value == "testid")
select e;
Run Code Online (Sandbox Code Playgroud)
我必须将resQ作为参数传递给下面的函数.为了做到这一点,我必须将resQ转换为XElement类型.
Database.usp_InsertTestNQuestions(tid, qId, qstn, ans, resQ );
Run Code Online (Sandbox Code Playgroud)
只要您的查询只返回单个结果,您就可以在结果上调用Single()或First()(此外,不需要额外的查询语法):
// Use if there should be only one value.
// Will throw an Exception if there are no results or more than one.
var resQ = docElmnt.Descendents(xmlns + Constants.T_ROOT)
.Single(x => x.Attribute(Constants.T_ID).Value == "testid");
// Use if there could be more than one result and you want the first.
// Will throw an Exception if there are no results.
var resQ = docElmnt.Descendents(xmlns + Contants.T_ROOT)
.First(x => x.Attribute(Constants.T_ID).Value == "testid");
Run Code Online (Sandbox Code Playgroud)
如果你想在没有抛出异常的情况下没有返回查询结果的情况下处理这种情况,你可以使用SingleOrDefault(如果你得到多个结果,它仍会抛出异常)或FirstOrDefault.