开发者

ADO.NET Entity Framework multiple statement in where clause or a grouped lambda expression

开发者 https://www.devze.com 2023-02-19 19:23 出处:网络
I am new to the ADO.NET Entity Framework Model. Using DotNet Framework 3.5 I have two tables : viz. customer开发者_开发问答s and city

I am new to the ADO.NET Entity Framework Model. Using DotNet Framework 3.5

I have two tables :

viz. customer开发者_开发问答s and city

The customers table refers to a cityname column in the table city (foreign key relationship)

While creating a win c# form i am giving the user to filter customers based on his search choices (viz. name, city, number, etc.) Here is my structure


using(DataContext dc = new DataContext())  
{  
    IEnumerable cust = dc.customers;  
    if(name != null)    {  
        cust = cust.Where<customers>(c => c.name == name);
    }  
    if(mobile != null)    {  
        cust = cust.Where<customers>(c => c.mobile == mobile);
    }  
    if(city != null)    {
        cust = cust.Where<customers>(c => c.city.cityname == city); //ERROR HERE
    }
}

I get a NullPointerException, since the EntityReference.Load method is not called upon. Quite logical point and I agree to it. I would like some advice on how to either call the load method in the current architechure. Is it possible to somehow do this :

    c.cityReference.Load();
    c.city.cityname == city

Or possibly some lambda expression (I am new to it) which induces both the statements? Ne suggestions? I am ready to change the current architecture if anyone has a better advice.


Try

IEnumerable cust = dc.customers.Include("city");  

Check out this post for reference.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号