LINQ 多条件查询怎么写?

kariya 2012-11-20 09:22:45
1.页面用TextBox输入查询信息
<table>
<tr>
<td>设备编号:</td>
<td>@Html.TextBox("deviceCode")
@Html.ValidationMessage("deviceCode")
</td>
</tr>
...
<table>
2.用linq来写查询语句
[HttpPost]
public ActionResult Search(FormCollection collection)
{
var q = from p in db.Devices select p;
if (!string.IsNullOrEmpty(collection["deviceCode"]))
q = q.Where(p => p.deviceCode == int.Parse(collection["deviceCode"]));
...
return View(q.ToList());
}
最后弹出LINQ to Entities 不识别方法“Int32 Parse(System.String)”,因此该方法无法转换为存储表达式。 究竟应该怎么写多条件查询?
...全文
3811 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
哥子谭 2014-05-20
  • 打赏
  • 举报
回复
原来如此,谢谢分享
jack123456789fdfd 2013-07-19
  • 打赏
  • 举报
回复
@model IEnumerable<StuApplication.Models.Student> @{ ViewBag.Title = "学生信息"; } <h2>学生信息</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <div class="editor-label"> 性别: @Html.DropDownList("list",ViewData["list"] as SelectList) </div> <p> <input type="submit" value="查询" /> </p> </fieldset> } <p> @*@Html.ActionLink("查询", "Index")*@ @Html.ActionLink("添加", "Create") </p> <table> <tr> <th> </th> <th> 照片 </th> <th> 姓名 </th> <th> 生日 </th> <th> 性别 </th> <th></th> </tr> 这是页面上面部分
jack123456789fdfd 2013-07-19
  • 打赏
  • 举报
回复
跪求大侠帮忙。
jack123456789fdfd 2013-07-19
  • 打赏
  • 举报
回复
我也是报这个错。你是在controller里面。我的是在view里面。 <table> <tr> <th> </th> <th> 照片 </th> <th> 姓名 </th> <th> 生日 </th> <th> 性别 </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> </td> <td> @Html.DisplayFor(modelItem => item.Photo) </td> <td> @Html.DisplayFor(modelItem => item.StuName) </td> <td> @Html.DisplayFor(modelItem => item.Borndate) </td> <td> @Html.DisplayFor(modelItem => item.Sex) </td> <td> @Html.ActionLink("编辑", "Edit", new { id = item.StuId }) | @Html.ActionLink("删除", "Delete", new { id = item.StuId }) | @Html.ActionLink("管理成绩", "Details", new { id=item.StuId }) </td> </tr> } </table> @foreach (var item in Model)这里报LINQ to Entities 不识别方法“System.String get_Item(System.String)
E次奥 2012-11-20
  • 打赏
  • 举报
回复
你把转换放到外边不就行了
q107770540 2012-11-20
  • 打赏
  • 举报
回复
那这样: public ActionResult Search(FormCollection collection) { var q = (from p in db.Devices select p).ToList(); if (!string.IsNullOrEmpty(collection["deviceCode"])) q = q.Where(p => p.deviceCode == int.Parse(collection["deviceCode"])).ToList(); ... return View(q); }
kariya 2012-11-20
  • 打赏
  • 举报
回复
这样我也试过又会报LINQ to Entities 不识别方法“Convert.ToInt32(System.String)”,因此该方法无法转换为存储表达式。 的错误
q107770540 2012-11-20
  • 打赏
  • 举报
回复
q = q.Where(p => p.deviceCode == Convert.ToInt32(collection["deviceCode"]));
q107770540 2012-11-20
  • 打赏
  • 举报
回复
一句话:.ToList()先将整个表的数据加载到内存中了 然后后边的查询都是LINQ TO Object查询了,已经不是LINQ TO EF了
kariya 2012-11-20
  • 打赏
  • 举报
回复
跟我一个等级的都这么牛,汗! 好,结题。下次有问题再散分
NET小工 2012-11-20
  • 打赏
  • 举报
回复
Linq是延迟加载的,一个Lambda语句结束时,生成的并不是结果集,而是一个SQL指令集,只有当程序请求该结果集的值的时候,才会实际执行该SQL指令查询出结果。 所以如果你的LinqToSQL语句中包含有无法转译为SQL的代码段的时候,就会抛出异常,这类错误通常发生于对筛选条件执行函数操作。 而ToList,AsEnumerable之类的方法都需要把数据实际压入新的数据结构中,执行这类方法会立即执行LinqToSQL语句,然后对生成的中间集进行Linq查询的时候,调用的是LinqToEntity的底层代码,不会再抛出SQL转译的异常。 楼主,40分啊,你懂的。
kariya 2012-11-20
  • 打赏
  • 举报
回复
不晚呀,又提了问题呀
NET小工 2012-11-20
  • 打赏
  • 举报
回复
版主不厚道啊,亲自出来抢分。
kariya 2012-11-20
  • 打赏
  • 举报
回复
出结果了,太感谢了!这个问题困扰我好几个星期,1.但我不明白为什么将.ToList()写在前面就对了?我的笨办法就是在网上搜帖子,东拼西凑写了这个程序,但很多地方不理解,2.能告诉我正确的方法吗?是要把LINQ学一遍吗? 不好意思问题太多,出结果了,很开心^!^
q107770540 2012-11-20
  • 打赏
  • 举报
回复
var q = (from p in db.Devices.ToList()
kariya 2012-11-20
  • 打赏
  • 举报
回复
最后我改成这样,还是报以下错误,出不来结果,为什么呀? LINQ to Entities 不识别方法“System.String get_Item(System.String)”,因此该方法无法转换为存储表达式。 var q = (from p in db.Devices where (string.IsNullOrEmpty(collection["deviceCode"])? true : p.deviceCode == Convert.ToInt32(collection["deviceCode"])) &&(string.IsNullOrEmpty(collection["deviceName"])?true: p.deviceName == collection["deviceName"]) &&(string.IsNullOrEmpty(collection["deviceModel"])?true:p.deviceModel == collection["deviceModel"]) &&(string.IsNullOrEmpty(collection["deviceStandard"])?true:p.deviceStandard == collection["deviceStandard"]) &&(string.IsNullOrEmpty(collection["capital"])?true:p.capital == collection["capital"]) &&(string.IsNullOrEmpty(collection["repairNumber"])?true:p.repairNumber == Convert.ToInt32(collection["repairNumber"])) &&(string.IsNullOrEmpty(collection["upgradeNumber"])?true:p.upgradeNumber == Convert.ToInt32(collection["upgradeNumber"])) &&(string.IsNullOrEmpty(collection["userID"])?true:p.userID == Convert.ToInt32(collection["userID"])) &&(string.IsNullOrEmpty(collection["position"])?true:p.position == collection["position"]) &&(string.IsNullOrEmpty(collection["state"])?true:p.state == collection["state"]) &&(string.IsNullOrEmpty(collection["deviceType"])?true:p.deviceType == collection["deviceType"]) orderby p.deviceID select p).Select(p=>new Device() { deviceID=p.deviceID, deviceCode = p.deviceCode, statePic = new byte[110010], deviceName=p.deviceName, deviceModel=p.deviceModel, deviceStandard=p.deviceStandard, devicePrice=p.devicePrice, supplierID=p.supplierID, producerID=p.producerID, contractID=p.contractID, projectID=p.projectID, capital=p.capital, repairNumber=p.repairNumber, upgradeNumber=p.upgradeNumber, userID=p.userID, guarantPeriod=p.guarantPeriod, position=p.position, deviceSN=p.deviceSN, repairCost=p.repairCost, upgradeCost=p.upgradeCost, outFactoryDate=p.outFactoryDate, purchaseDate=p.purchaseDate, accountDate=p.accountDate, handlerantID=p.handlerantID, requisitionID=p.requisitionID, uselessID=p.uselessID, transferID=p.transferID, loseID=p.loseID, appraiseNumber=p.appraiseNumber, circulateNumber=p.circulateNumber, bidding=p.bidding, state=p.state, deviceType=p.deviceType }).ToList();

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧