Linq to sql 如何为linq to sql属性编写表达式?

Linq to sql 如何为linq to sql属性编写表达式?,linq-to-sql,Linq To Sql,我对这个冗长的问题表示赞同。我尽了很大的努力一次把我的问题弄清楚。请容忍我;o) 任何帮助都将不胜感激 我有课程分支和课文: class Branch int ID Text WebDescription and a bunch of other properties class Text int ID string UK string NL string FR string IT and a bunch of other properties as well 我只想用适当

我对这个冗长的问题表示赞同。我尽了很大的努力一次把我的问题弄清楚。请容忍我;o) 任何帮助都将不胜感激

我有课程分支和课文:

class Branch
 int ID
 Text WebDescription
 and a bunch of other properties

class Text
 int ID
 string UK
 string NL
 string FR
 string IT
 and a bunch of other properties as well
我只想用适当的语言显示分支的ID及其描述。我只需要一个查询(没有额外的往返),它只检索两个字段(不是整个对象)

我找到了三个解决办法

通过查询中的对象模型

// good: no round trips
// good: clean sql
// bad:  impossible to use the currentUserLanguage parameter
var lang = "NL";
var dbProject = new ProjectDataContext();
var query = from b in dbProject.GetTable<Branch>()
            select new
               {
                 b.ID,
                 WebDescription = b.WebDescriptionObject.NL // <-- problem
               };
var text = query.First().WebDescription;
// good: no round trips (eager loading of text object)
// good: possible to use the currentUserLanguage parameter
// bad:  loads the *whole* branch and text object, not just two fields
var lang= "NL";
var dbProject = new ProjectDataContext();
var query = from b in dbProject.GetTable<Branch>()
            select new
              {
                b.ID,
                WebDescription = b.GetWebDescriptionAsString(lang)
              };
var text = query.First().WebDescription;
//很好:没有往返
//好:干净的sql
//错误:无法使用currentUserLanguage参数
var lang=“NL”;
var dbProject=newprojectdatacontext();
var query=来自dbProject.GetTable()中的b
选择新的
{
b、 身份证,

WebDescription=b.WebDescriptionObject.NL/如果使用存储过程,这将非常容易做到。您反对将SP用作解决方案吗?

如果使用存储过程,这将非常容易做到。您反对将SP用作解决方案吗?

如果存储过程有效,那么我很乐意使用它。

如果存储过程有效过程是有效的,那么我很乐意使用它。

没有真正回答这个问题,最干净的方法是将文本结构更改为更规范的形式,如:

Text
    ID

TextTranslation
    ID
    TextID
    Lang
    TextValue
其中每个文本都有多个翻译,每种语言一个

查询将类似于:

var q = 
    from branch in dbProject.Branches
    join text in dbProject.Texts on branch.TextID = text.ID
    join translation in dbProject.TextTranslations on text.ID = translation.TextID
    where translation.Lang == lang
    select new
    {
        branch.ID,
        WebDescription = translation.TextValue
    };

这种方法还有其他优点,例如添加新语言不会改变模型结构。

如果不真正回答这个问题,最干净的方法是将文本结构更改为更规范的形式,如:

Text
    ID

TextTranslation
    ID
    TextID
    Lang
    TextValue
其中每个文本都有多个翻译,每种语言一个

查询将类似于:

var q = 
    from branch in dbProject.Branches
    join text in dbProject.Texts on branch.TextID = text.ID
    join translation in dbProject.TextTranslations on text.ID = translation.TextID
    where translation.Lang == lang
    select new
    {
        branch.ID,
        WebDescription = translation.TextValue
    };

这种方法还有其他优点,例如,添加一种新语言不会改变模型结构。

不了解您的整个问题

创建如下所示的存储过程:

CREATE PROCEDURE spGetTheTextINeed @Language char(2), @BranchID int
AS
  /* I don't know how your database is structured so you need to write this */
  SELECT MyText from MyTable WHERE Language=@Language and Branch=@BranchID

然后需要将sp添加到DBML中,然后只需使用适当的参数调用所需的sp即可:

var query = myDataContext.spGetTheTextINeed("NL",[your branch number]) 

  Dim str As String 
  str = query.MyText 

上面的代码并不准确-我不了解您的全部需求,但这应该可以让您开始。

但不了解您的整个问题

创建如下所示的存储过程:

CREATE PROCEDURE spGetTheTextINeed @Language char(2), @BranchID int
AS
  /* I don't know how your database is structured so you need to write this */
  SELECT MyText from MyTable WHERE Language=@Language and Branch=@BranchID

然后需要将sp添加到DBML中,然后只需使用适当的参数调用所需的sp即可:

var query = myDataContext.spGetTheTextINeed("NL",[your branch number]) 

  Dim str As String 
  str = query.MyText 

上面的代码并不准确-我不了解您的全部要求,但这应该可以让您开始了。

感谢您的及时回复

我做了一个快速的尝试。UDF已经存在,我只是不知道如何使用它。性能显著下降。第一个解决方案的速度快了3倍。据我所知,这种方法需要额外的数据库往返。对吗

var query = from b in dbProject.GetTable<Branch>()
            select new
              {
                b.ID,
                WebDescription = db.fGetText(b.WebDescriptionID, (currentUserLanguage))
              };
var query=来自dbProject.GetTable()中的b
选择新的
{
b、 身份证,
WebDescription=db.fGetText(b.WebDescriptionID,(currentUserLanguage))
};

感谢您的及时回复

我做了一个快速的尝试。UDF已经存在,我只是不知道如何使用它。性能显著下降。第一个解决方案的速度快了3倍。据我所知,这种方法需要额外的数据库往返。对吗

var query = from b in dbProject.GetTable<Branch>()
            select new
              {
                b.ID,
                WebDescription = db.fGetText(b.WebDescriptionID, (currentUserLanguage))
              };
var query=来自dbProject.GetTable()中的b
选择新的
{
b、 身份证,
WebDescription=db.fGetText(b.WebDescriptionID,(currentUserLanguage))
};

是的,我知道。但sql专家向我们承诺,这是一种性能明智的方法(不确定他是否正确)。无论如何,改变结构已不再是选项。我非常确定他是不对的:)解决方案是将条件直接放入查询中,如:WebDescription=lang==“NL”?b.Text.NL:lang==“FR”?b.Text.FR:…没有测试它,但我认为它会生成正确的sql。是的,我知道。但sql专家向我们承诺,这是一种性能明智的方法(不确定他是否正确)。无论如何,更改结构已不再是选项。我很确定他是不对的:)解决方案是将条件直接放入查询中,如:WebDescription=lang==“NL”?b.Text.NL:lang==“FR”?b.Text.FR:…没有测试它,但我认为它会生成一个正确的sql。不清楚您要问的是谁/什么…您是试图返回所有的分支id,然后是它们的描述,还是一个分支和一个描述?不清楚您要问的是谁/什么…您是试图返回所有的分支id,然后是它们的描述,还是一个分支和一个描述?