linq:在 select 子句中使用方法

发布于 2024-11-16 04:30:28 字数 1514 浏览 4 评论 0原文

我对此感到非常困惑,并决定与您分享我的问题 我想从多个表中创建一个匿名选择,其中一些表可能包含多个结果。我想将这些结果连接成一个字符串 我做了这样的事情:

var resultTable = from item in dc.table
                    select new
                    {
                      id= item.id,
                      name= CreateString((from name in item.Ref_Items_Names
                               select name.Name).ToList()),
                    };

CreateString() 是:

private string CreateString(List<string> list)
{
  StringBuilder stringedData = new StringBuilder();
  for (int i = 0; i < list.Count; i++)
  {
    stringedData.Append(list[i] + ", ");
  }
  return stringedData.ToString();
}

我的意图是将“name”查询转换为列表,然后将其发送到 CreateString() 以将其转换为一个长连接字符串。

我尝试使用 .Aggregate((current,next) => current + "," + next); 但是当我尝试将查询转换为 DataTable 时,如下所示:

public DataTable ToDataTable(Object query)
{
  DataTable dt = new DataTable();
  IDbCommand cmd = dc.GetCommand(query as IQueryable);
  SqlDataAdapter adapter = new SqlDataAdapter();
  adapter.SelectCommand = (SqlCommand)cmd;
  cmd.Connection.Open();
  adapter.Fill(dt);
  cmd.Connection.Close();
  return dt;
}

我收到异常“dc.GetCommand()”无法理解使用聚合方法的查询 后来我尝试使用这个简单的查询:

var resultTable = from itemin dc.table
                    select new
                    {
                      name = CreateString()
                    };

当 CreateString() 返回“成功”时,没有任何内容插入到“名称”中 为什么没有办法在 select 子句中使用方法? 谢谢 约塔姆

I'm breaking my head with this and decided to share my problem with you
I want to create an anonymous select from several tables, some of them may contain more than one result. i want to concatenate these results into one string
i did something like this:

var resultTable = from item in dc.table
                    select new
                    {
                      id= item.id,
                      name= CreateString((from name in item.Ref_Items_Names
                               select name.Name).ToList()),
                    };

and the CreateString() is:

private string CreateString(List<string> list)
{
  StringBuilder stringedData = new StringBuilder();
  for (int i = 0; i < list.Count; i++)
  {
    stringedData.Append(list[i] + ", ");
  }
  return stringedData.ToString();
}

my intentions were to convert the "name" query to list and then sent it to CreateString() to convert it to one long concatenated string.

I tried using .Aggregate((current,next) => current + "," + next);
but when i try to convert my query to DataTable like below:

public DataTable ToDataTable(Object query)
{
  DataTable dt = new DataTable();
  IDbCommand cmd = dc.GetCommand(query as IQueryable);
  SqlDataAdapter adapter = new SqlDataAdapter();
  adapter.SelectCommand = (SqlCommand)cmd;
  cmd.Connection.Open();
  adapter.Fill(dt);
  cmd.Connection.Close();
  return dt;
}

I'm getting exception that "dc.GetCommand()" can't understand query with Aggregate method
later I tried to even use this simple query:

var resultTable = from itemin dc.table
                    select new
                    {
                      name = CreateString()
                    };

When CreateString() returns "success", nothing was inserted to "name"
why there is no way of using methods in select clause?
Thank you
Yotam

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

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

发布评论

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

评论(2

—━☆沉默づ 2024-11-23 04:30:28

LINQ to object 和 LINQ to some-db-provider 之间存在差异。一般来说,使用 IQueryable 时,您不能使用任何方法,除了您的提供程序理解的方法。

您可以做的是从数据库中检索数据,然后使用 LINQ to object 进行格式化:

var data = from item in dc.table
           where /* some condition */
           select item;

var result = from item in data.AsEnumerable()
             select new
             {
                 name = SomeFunction(item)
             }

AsEnumerable() 扩展方法强制使用 LINQ to 对象进行处理。

There is difference between LINQ to objects and LINQ to some-db-provider. Generally speaking, when using IQueryable, you can't use any methods, except the ones your provider understands.

What you can do is to retrieve the data from the database and then do the formatting using LINQ to objects:

var data = from item in dc.table
           where /* some condition */
           select item;

var result = from item in data.AsEnumerable()
             select new
             {
                 name = SomeFunction(item)
             }

The AsEnumerable() extension method forces processing using LINQ to objects.

无所的.畏惧 2024-11-23 04:30:28

如果我没有解释你的问题,请原谅我。看来您想要做的就是抽象您的 select 方法以供重用。如果是这种情况,您可以考虑使用 lambda 表达式进行投影。例如:

internal static class MyProjectors
{
    internal static Expression<Func<Object1, ReturnObject>> StringDataProjector
    {
        get
        {
            return d => new Object1()
            {
                //assignment here
            }
        }
    }
}

现在您可以这样选择数据集:

dc.Table.Select(MyProjectors.StringDataProjector)

至于串联逻辑,选择具有 IEnumerable 属性和只读属性的某个基类来处理以下内容的串联怎么样?字符串?

Forgive me if I've miss interpreted your question. It seems that what you are trying to do is abstract your select method for reuse. If this is the case, you may consider projection using a lambda expression. For example:

internal static class MyProjectors
{
    internal static Expression<Func<Object1, ReturnObject>> StringDataProjector
    {
        get
        {
            return d => new Object1()
            {
                //assignment here
            }
        }
    }
}

Now you can select your datasets as such:

dc.Table.Select(MyProjectors.StringDataProjector)

As for the concatenation logic, what about selecting to some base class with an IEnumerable<string> property and a read-only property to handle the concatenation of the string?

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