English 中文(简体)
为什么我通过Lambda明示in Func,给我一个错误
原标题:Why when I pass a Lambda expressin Func thats give me an error

When I pass call a Generic Method that return A Func and pass in parameter of the Where, that s dosen t work. (System.InvalidOperationException: Internal .NET Framework Data Provider error 1025.) The error is when I want to get the Role information.
For the Role, I need to perform a Where Clause Expression EX: (p => p.LangID == 1)

www.un.org/Depts/DGACM/index_spanish.htm 该法典有:t Work

存放处

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 />");
                }

www.un.org/Depts/DGACM/index_spanish.htm 本条

 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 />");
            }

我的联系人

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>
最佳回答

na笑,你需要使用富 Fun语,而不是直言 Fun:

public Expression<Func<T, bool>> GetLmbLang<T>() where T:class,IBaseGenericTxt
{
    int lang = -1;
    lang = Convert.ToInt32(HttpContext.Current.Session["Language"]);
    return (p => p.LangID == lang);
}

<><>Edit>/strong>

Ah, 是的,问题在于,你的职能实际上并不了解它在编纂时的工作类别:它只是知道它属于一个类别,而且它执行的是国际基因组。 Txt。 因此,当你说<代码>p.LangId时,该部分表述是称IBaseGenericTxt.LangId,而不是联系TypeText。 LangId。

你们需要建设自己的表达树,以便正确工作。 与此类似:

var paramExpr = Expression.Parameter(typeof(T), "p");
return Expression.Lambda<Func<T, bool>>(
    Expression.Equal(
        Expression.Property(paramExpr, "LangId"),
        Expression.Constant(lang)),
    paramExpr);

www.un.org/Depts/DGACM/index_spanish.htm Edit 2

两点:

  1. Because LINQ to Entities will try to take anything in a query expression and convert it to a SQL statement, you have to be careful not to go calling methods in the middle of your query. You ll want to call the GetLmbLang method first and store its value in a variable to use in the query.
  2. 正如你在评论中指出的,由于接触TypeTexts财产不能执行可贵的,这尤其容易渗透。 你有三种选择,我可以说明:

    1. Create your entire select statement as an expression tree. This is very annoying and error-prone.
    2. Use Joe Albari s LinqKit to "Compile" and "Expand" your query. LinqKit will traverse the expression tree and build a new tree wherein your query Expression is converted to its equivalent Func.
    3. Go back to your data context rather than using the ContactTypeTexts property.

从个人角度看,我也许会接受最后的选择,例如:

var lambdaLang = repGeneric.GetLmbLang<ContactTypeText>();
var ViewModel = _db.Contacts
    .Where(a=> a.IsActive == true)
    .Select(a => new ContactListViewModel { 
    ContactID = a.ContactID,
    ContactName = a.ContactName,
    Role = _db.ContactTypeTexts
        .Where(ct => ct.ContactType.Contacts.Any(
            c => c.ContactId == a.ContactId)
        .Where(lambdaLang)
        .Select(af => af.Txt).FirstOrDefault(),
    CompanyType = a.Supplier.SupplierName,
    Addr = a.Address ,
    Email = a.ContactEmail,
    Phone = a.ContactPhone
}).ToList();
问题回答

后一部法典之所以发挥作用,是因为C#汇编者将其转化成一种表达的树木。 系统。 表达方式,而您的原始法典则被汇编成一个 Fun。 目前设计的Linq 不能处理Func树,只能说树木。





相关问题
Multiple Where clauses in Lambda expressions

I have a simple lambda expression that goes something like this: x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty) Now, if I want to add one more where clause to the ...

Lambda Expressions and Event Subscriptions

I ve heard that if lambda expressions are used to subscribe to an event, then this creates a weak reference to the event handler code, so it is not required to explicitly unsubscribe from the event ...

Weak event handler model for use with lambdas

OK, so this is more of an answer than a question, but after asking this question, and pulling together the various bits from Dustin Campbell, Egor, and also one last tip from the IObservable/Rx/...

Is there a way to specify an "empty" C# lambda expression?

I d like to declare an "empty" lambda expression that does, well, nothing. Is there a way to do something like this without needing the DoNothing() method? public MyViewModel() { SomeMenuCommand =...

C# Lambda expression -Help

I am learning lambda expression and delegates.While i try to execute the following ,I am getting error at the line which is marked bold line. (Error : Operator += cannot be applied to operands of ...

LINQ to append to a StringBuilder from a String[]

I ve got a String array that I m wanting to add to a string builder by way of LINQ. What I m basically trying to say is "For each item in this array, append a line to this StringBuilder". I can do ...

热门标签