在LINQ-TO-SQL中,如何联合或联接到不来自数据库的文本行?
我试图通过连接到一行文本而不是表中的数据来扩展结果。在LINQ-TO-SQL中,如何联合或联接到不来自数据库的文本行?,linq-to-sql,Linq To Sql,我试图通过连接到一行文本而不是表中的数据来扩展结果。 例如: from c in db.customers join x in ( select new { custType = "A" } ).Union( select new { custType = "B" } ).Union( select new { custType = "C" } ) on 1 equals 1 select new { c.CustomerName
例如:
from c in db.customers
join x in (
select new
{
custType = "A"
}
).Union(
select new
{
custType = "B"
}
).Union(
select new
{
custType = "C"
}
)
on 1 equals 1
select new
{
c.CustomerName,
x.custType
}
这将提供三次客户列表,每个客户指定了不同的客户类型。如何根据示例创建这个临时“x”表
我尝试指定一个实际的表,但如果我没有从表中返回任何行,则会出现错误:
成员'f_uuAnonymousTyped7'2[System.String,System.String].custType'
没有支持的到SQL的转换
你试过这样的东西吗
var queryA = from customer in db.customers
select new {
customer.CustomerName,
custType = "A"
};
var queryB = from customer in db.customers
select new {
customer.CustomerName,
custType = "B"
};
var queryC = from customer in db.customers
select new {
customer.CustomerName,
custType = "C"
};
var result = queryA.Union(queryB).Union(queryC);
您可能希望创建一个包含客户名称和类型属性的类,而不是依赖于匿名属性。这是一种偏好选择,以及它将被重复使用的位置。您尝试过类似的方法吗
var queryA = from customer in db.customers
select new {
customer.CustomerName,
custType = "A"
};
var queryB = from customer in db.customers
select new {
customer.CustomerName,
custType = "B"
};
var queryC = from customer in db.customers
select new {
customer.CustomerName,
custType = "C"
};
var result = queryA.Union(queryB).Union(queryC);
您可能希望创建一个包含客户名称和类型属性的类,而不是依赖于匿名属性。这是一种偏好选择,以及它将被重复使用的位置。是的,这是一种可能性。我发布的示例过于简单,因此我可以在更复杂的查询中显示我想要做的事情。我可能不得不像你所展示的那样尝试打破它。我还希望通过只运行一个查询并限制我的联接返回的结果来提高性能。我想需要注意的是,您希望尽可能利用延迟执行。与传统的SQL不同,使用LINQ时,将查询构建成更小、简单的部分,这样就很容易理解,是的,这是一种可能性。我发布的示例过于简单,因此我可以在更复杂的查询中显示我想要做的事情。我可能不得不像你所展示的那样尝试打破它。我还希望通过只运行一个查询并限制我的联接返回的结果来提高性能。我想需要注意的是,您希望尽可能利用延迟执行。与传统的SQL不同,使用LINQ时,将查询构建为更小、简单的部分,以便易于遵循,这通常是有意义的。