执行查询<>具有层次结构对象和 LINQ to SQL

发布于 2024-09-17 08:58:51 字数 461 浏览 2 评论 0 原文

假设我有两个对象:

public class Person{
     string Name { get; set;}
     Address Home { get; set;}
}

public class Address{
     string Street{ get; set;}
     string City { get; set;}
}


    string sql = "Select Name, Home_street, Home_city from user";
    var results = dc.ExecuteQuery<Person>(sql);

这里的问题是家乡街道和家乡城市没有人口。有什么办法可以让它发挥作用吗?我认为可以通过使用关联属性来解决,但不确定如何解决。

如果我使用存储过程,我也会遇到同样的问题。我正在尝试构建一个查询工具,因此很多变量无法提前知道。

Let's say I have two objects:

public class Person{
     string Name { get; set;}
     Address Home { get; set;}
}

public class Address{
     string Street{ get; set;}
     string City { get; set;}
}


    string sql = "Select Name, Home_street, Home_city from user";
    var results = dc.ExecuteQuery<Person>(sql);

The problem here is that the Home street and home city are not populated. Is there a way I can make it work? I think it's solvable by using Association attribute, but not sure how.

I have the same problem if I use stored procedure. I'm trying to build a query tool so a lot of variables are not known ahead of time.

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

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

发布评论

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

评论(1

温暖的光 2024-09-24 08:58:51

您需要制作一个具有平面形状的数据对象来捕获结果,然后将这些结果投影到您想要的任何形状。

public class PersonQueryResult
{
  string Name {get;set;}
  string Home_street {get;set;}
  string Home_city {get;set;}
}

string sql = "Select Name, Home_street, Home_city from user";
List<PersonQueryResult> results = dc.ExecuteQuery<PersoneQueryResult(sql).ToList();
List<Person> projectedResults = results.Select(pqr => new Person()
  {
    Name = pqr.Name,
    Home = new Address()
    {
      Street = pqr.Home_street,
      City = pqr.Home_city
    }
  }
).ToList();

如果有动态生成的类,您会如何处理它们?这并不是说人们可以针对这些编写经过编译器检查的代码。

我认为 ADO.NET 的非编译器检查世界会更合适地解决这个问题。 也就是说

string queryString = 
  "SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");

You need to make a data object with a flat shape to catch the results, then project those results into whatever shape you want.

public class PersonQueryResult
{
  string Name {get;set;}
  string Home_street {get;set;}
  string Home_city {get;set;}
}

string sql = "Select Name, Home_street, Home_city from user";
List<PersonQueryResult> results = dc.ExecuteQuery<PersoneQueryResult(sql).ToList();
List<Person> projectedResults = results.Select(pqr => new Person()
  {
    Name = pqr.Name,
    Home = new Address()
    {
      Street = pqr.Home_street,
      City = pqr.Home_city
    }
  }
).ToList();

What would you do with dynamically generated classes if you had them? It's not as though one can write compiler-checked code against those.

I would think the non-compiler-checked world of ADO.NET would solve this problem more appropriately. To wit.

string queryString = 
  "SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

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