ADO.NET 实体框架 where 子句或分组 lambda 表达式中的多个语句

发布于 2024-10-27 08:10:45 字数 935 浏览 1 评论 0 原文

我是 ADO.NET 实体框架模型的新手。使用 DotNet Framework 3.5

我有两个表:

即。 customerscity

客户表引用城市表中的城市名称列(外键关系)

在创建 win c# 表单时,我让用户根据以下内容过滤客户他的搜索选择(即名称、城市、号码等)这是我的结构


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
    }
}

我得到一个 NullPointerException,因为没有调用 EntityReference.Load 方法。非常合乎逻辑的观点,我同意。 我想要一些关于如何在当前架构中调用加载方法的建议。是否有可能以某种方式做到这一点:

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

或者可能是一些 lambda 表达式(我是新手)会引发这两个语句?有什么建议吗? 如果有人有更好的建议,我准备改变当前的架构。

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

I have two tables :

viz. customers 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2024-11-03 08:10:45

尝试

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

检查 出此帖子供参考。

Try

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

Check out this post for reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文