LINQ TO SQL AND LINQ TO XML联合查询遇到的有关问题
发布时间:2011-06-24 21:53:56 文章来源:www.iduyao.cn 采编人员:星星草
LINQ TO SQL AND LINQ TO XML联合查询遇到的问题
在做LINQ TO SQL AND LINQ TO XML联合查询时遇到一个奇怪的问题,情况如下
这样做是没问题的
BookDataContext data = new BookDataContext();
XElement xe = XElement.Load(Server.MapPath("myxml.xml"));
var q=from b in xe.Descendants("book")
join t in data.booktype on (int)b.Attribute("booktype") equals t.id
select new {
id=(string)b.Element("id"),
bookname = (string)b.Element("bookname"),
publish = (string)b.Element("publish"),
price=(string)b.Element("price"),
booktype=t.typename
};
foreach (var qq in q)
{
Response.Write(qq.id + "--" + qq.bookname + "--" + qq.price + "--" + qq.publish + "--" + qq.booktype + "<br>");
}
但是对调一下位置就出现问题了(报错为 :不能在查询运算符(Contains() 运算符除外)的 LINQ to SQL 实现中使用本地序列。 )
BookDataContext data = new BookDataContext();
XElement xe = XElement.Load(Server.MapPath("myxml.xml"));
var q = from t in data.booktype
join b in xe.Descendants("book") on t.id equals (int)b.Attribute("booktype")
select new
{
id = (string)b.Element("id"),
bookname = (string)b.Element("bookname"),
publish = (string)b.Element("publish"),
price = (string)b.Element("price"),
booktype = t.typename
};
foreach (var qq in q)
{
Response.Write(qq.id + "--" + qq.bookname + "--" + qq.price + "--" + qq.publish + "--" + qq.booktype + "<br>");
}
真是奇怪了
var q=from b in xe.Descendants("book")
join t in data.booktype on (int)b.Attribute("booktype") equals t.id (没问题)
var q = from t in data.booktype
join b in xe.Descendants("book") on t.id equals (int)b.Attribute("booktype") (报错)
为什么会出现这种情况呢?还有报错的那个做法要怎么修改?请高手们帮忙解答啊,多谢了!
------解决方案--------------------
var q = from t in data.booktype.ToList()
join b in xe.Descendants("book") on t.id equals (int)b.Attribute("booktype") into bb
from b in bb.DefaultIfEmpty()
select new {
id=b==null?"0":(string)b.Element("id"),
bookname =b==null?"": (string)b.Element("bookname"),
publish =b==null?"": (string)b.Element("publish"),
price=b==null?"":(string)b.Element("price"),
booktype=t.typename
};
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
LINQ 方法参数看不懂
怎么我在编译器写代码LINQ方法参数提示看不懂啊,比如 GroupBy(Of TSource, TKey)(IQueryable(Of TSource), E...
-
LINQ 对GridView分页排序处理?
我始终觉得LINQ对gridview分页排序操作很麻烦,请问有什么好的办法啊?还有以前写SQL语句,调试时候很方...
-
Mvc强类型列表如何修改字段
我用这个循环了一个文章列表,但是我要把文章内容字段的html代码给剔除掉,请问怎么做
------解决方案-...
-
Linq查询List<T>无值
List<Wisesoft.Tool.Log.Model.LoginLogInfo> listLog = new List<Wisesoft.Tool.Log.Model.LoginLogInfo>(...
-
SQL中的 is null 在linq中怎么表示
各位大哥,小弟想问下在 sql中的 where coulumn is null这个语句用linq应该怎么写,急用
------...
-
linq + GridView页面数据没有显示
protected void Page_Load(object sender, EventArgs e)
{
//Database db = new...
-
linq to sql 查询的问题,求助
对linq 还不是很熟悉,接触linq了,就有一种冲动要使用它,不用ado了,可是前段时间写一个linq 查询语句的...
-
请教各位 创建SQL server数据库的查询表达式
各位,我那本教程上说,创建一DBML文件,之后什么“创建一个SQL server数据库类型的数据源...
-
类型“System.String[]”不支持比较运算符。
return dc.Content.Where(c => c.Status == status && c.AuditList.Split(',')...
-
请教几个问题linq和数据绑定
1.用vs的O/R设计器生成的Linq to sql 的对象模型会为每个类添加 PropertyChangedEventHandler Prope...