DataContext - 通过 LINQ 查询时自动加载可选引用值

发布于 2024-11-02 16:12:13 字数 461 浏览 0 评论 0原文

我有以下情况(例如)

我有一个名为:Master.dbml 的 DataContext

它有 2 个表:

  • Hobby {id;name;}
  • HobbyReference {id;Hobby_Name;Hobby_Good_Name;}

当我查询 Hobby 时,DataContext 应该检查是否(伪代码):

 Hobby.Name EXISTS in HobbyReference.Hobby_Name
 THEN 
      take HobbyReference.Hobby_Good_Name
 ELSE 
      take Hobby.Hobby_Name
 END IF

对此的最佳实践是什么?

我知道如何做到这一点(扩展数据上下文),但我不知道如何完全实现它。

我该怎么做?

I have the following situation (as example)

I have a DataContext named : Master.dbml

It has 2 tables:

  • Hobby {id;name;}
  • HobbyReference {id;Hobby_Name;Hobby_Good_Name;}

Always when i query Hobby, the DataContext should check if (pseudo-code):

 Hobby.Name EXISTS in HobbyReference.Hobby_Name
 THEN 
      take HobbyReference.Hobby_Good_Name
 ELSE 
      take Hobby.Hobby_Name
 END IF

What's the best practice arround this?

I have an idea of how to do it (extending the datacontext), but i don't know how to fully implement it.

How would i do this?

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

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

发布评论

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

评论(2

囚我心虐我身 2024-11-09 16:12:13

Hobby.Hobby_Name 不存在!

无论如何,要在从 datacontext 返回之前进行检查,您可以执行以下操作:

public class HobbyDataService
    {
        MasterDataContext db = null;

        public HobbyDataService(string connection)
        {
            db = new MasterDataContext(connection);
        }

        internal string GetHoppyName(string hobbyName)
        {
            var x = from hr in this.db.HobbyReferences
                    where string.Equals(hr.Hoppy_Name, hobbyName)
                    select hr;
            if (x.Any())
                return x.First().Hobby_Good_Name;
            else
            {
                //Return what ever you want here
            }
        }
    }

 public partial class Hobby
    {
        public static string GetName(string hobbyName, string connection)
        {
            if (String.IsNullOrEmpty(hobbyName))
                throw new ArgumentException("hobbyName is null or empty.", "hobbyName");
            if (String.IsNullOrEmpty(connection))
                throw new ArgumentException("connection is null or empty.", "connection");

            HobbyDataService dataSrvce = new HobbyDataService(connection);
            return dataSrvce.GetHoppyName(hobbyName);
        }
    }

Hobby.Hobby_Name not exists!

anyway to check before return from datacontext you can do the following:

public class HobbyDataService
    {
        MasterDataContext db = null;

        public HobbyDataService(string connection)
        {
            db = new MasterDataContext(connection);
        }

        internal string GetHoppyName(string hobbyName)
        {
            var x = from hr in this.db.HobbyReferences
                    where string.Equals(hr.Hoppy_Name, hobbyName)
                    select hr;
            if (x.Any())
                return x.First().Hobby_Good_Name;
            else
            {
                //Return what ever you want here
            }
        }
    }

 public partial class Hobby
    {
        public static string GetName(string hobbyName, string connection)
        {
            if (String.IsNullOrEmpty(hobbyName))
                throw new ArgumentException("hobbyName is null or empty.", "hobbyName");
            if (String.IsNullOrEmpty(connection))
                throw new ArgumentException("connection is null or empty.", "connection");

            HobbyDataService dataSrvce = new HobbyDataService(connection);
            return dataSrvce.GetHoppyName(hobbyName);
        }
    }
傲世九天 2024-11-09 16:12:13

如果 HobbyReference 上的每个名称从来没有超过一个条目,并且如果这是用于查询(即不是用于更新),那么您可以执行类似这样的操作,

public class AmendedHobby
{
    public int Id  { get; set ;} 
    public string Name{ get; set ;} 
}

public IQueryable<AmendedHobby>  GetAmendedHobbies()
{
    return 
     (from h in Hobby
      join hr in HobbyRefernce on h.Name equals hr.Name into hrResults
      from hr in hrResults.DefaultIfEmpty()
      select new { h.id , Name = hr.Hobby_Good.Name ?? r.Name}  
}

这应该允许您执行子查询,例如

 (from r in GetAmendedHobbies() where r.Name == "Football" select r)

它将返回 AmishedHobby 类,其中,因为它没有链接到现有表,意味着更改不会保留回数据库。

If there is never more than one entry on HobbyReference for each name, and if this is used for querying (ie not for updating) then you can do something like

public class AmendedHobby
{
    public int Id  { get; set ;} 
    public string Name{ get; set ;} 
}

public IQueryable<AmendedHobby>  GetAmendedHobbies()
{
    return 
     (from h in Hobby
      join hr in HobbyRefernce on h.Name equals hr.Name into hrResults
      from hr in hrResults.DefaultIfEmpty()
      select new { h.id , Name = hr.Hobby_Good.Name ?? r.Name}  
}

This should allow you to do subqueries such as

 (from r in GetAmendedHobbies() where r.Name == "Football" select r)

It will return the AmendedHobby class which, as it is not linked to an existing table, means that changes won't be persisted back to the database.

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