【发布时间】:2010-11-14 04:58:01
【问题描述】:
当我调用一个返回 A Func 的通用方法并传入 Where 的参数时,这是行不通的。 (System.InvalidOperationException:内部 .NET Framework 数据提供程序错误 1025。)
错误是当我想获取角色信息时。
对于角色,我需要执行 Where 子句表达式 EX:(p => p.LangID == 1)
此代码不起作用
在仓库中
public Func<T, bool> GetLmbLang<T>() where T:class,IBaseGenericTxt
{
int lang = -1;
lang = Convert.ToInt32(HttpContext.Current.Session["Language"]);
return (p => p.LangID == lang);
}
在控制器中
var ViewModel = _db.Contacts.Where(a=> a.IsActive == true).Select(a => new ContactListViewModel {
ContactID = a.ContactID,
ContactName = a.ContactName,
Role = a.ContactType.ContactTypeTexts.Where(repGeneric.GetLmbLang<ContactTypeText>()).Select(af => af.Txt).FirstOrDefault(),
CompanyType = a.Supplier.SupplierName,
Addr = a.Address ,
Email = a.ContactEmail,
Phone = a.ContactPhone
}).ToList();
for (int i = 0; i < ViewModel.Count(); i++)
{
Response.Write(ViewModel.ElementAt(i).ContactID + "<br />");
}
此代码有效
int lang = -1;
lang = Convert.ToInt32(Session["Language"]);
var ViewModel = _db.Contacts.Where(a=> a.IsActive == true).Select(a => new ContactListViewModel {
ContactID = a.ContactID,
ContactName = a.ContactName,
Role = a.ContactType.ContactTypeTexts.Where(p => p.LangID == lang).Select(af => af.Txt).FirstOrDefault(),
CompanyType = a.Supplier.SupplierName,
Addr = a.Address ,
Email = a.ContactEmail,
Phone = a.ContactPhone
}).ToList();
for (int i = 0; i < ViewModel.Count(); i++)
{
Response.Write(ViewModel.ElementAt(i).ContactID + "<br />");
}
我的 ContactListViewModel
public class ContactListViewModel
{
public int ContactID { get; set; }
public string ContactName { get; set; }
public string Role { get; set; }
public string Company { get; set; }
public string CompanyType { get; set; }
public Address Addr { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
我的列表视图
..... Inherits="System.Web.Mvc.ViewPage<List<mvcinfosite.ViewModels.ContactListViewModel>>" %>
<table class="genTable">
<% for (int i = 0;i < Model.Count; i++) { %>
<tr>
<td>
<%: Html.ActionLink(item.ContactName, "Edit", new { id=item.ContactID }) %>
</td>
<td>
<%: item.Role %>
</td>
<td>
<%: item.Company %>
</td>
<td>
<%: item.CompanyType %>
</td>
<td>
<%: GlobalHelper.GetAddress(item.Addr) %>
</td>
<td>
<%: item.Email %>
</td>
<td>
<%: item.Phone %>
</td>
</tr>
<% } %>
</table>
【问题讨论】:
-
非常混乱的代码。你在做
_db.Contacts.ToList()(基本上是抓取所有联系人),然后对他们处于活动状态的联系人进行 another 查询,做AsQueryable()(这是多余的),但我认为主要问题是你的尝试在您的投影中进行内联查询(即 Role 属性)。也许如果您解释您要做什么,我们可以帮助您编写更好的查询。 -
我想用我所有的活跃联系人填满一个表格。你是对的 AsQueryable()。我做了很多测试,只是忘记在我的帖子中删除它。角色是一个字符串,我需要从我的联系人类型文本中获取此值。我将更新我的原始帖子以添加详细信息
标签: asp.net-mvc-2 entity-framework-4 lambda