linq:在 select 子句中使用方法
我对此感到非常困惑,并决定与您分享我的问题 我想从多个表中创建一个匿名选择,其中一些表可能包含多个结果。我想将这些结果连接成一个字符串 我做了这样的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LINQ to object 和 LINQ to some-db-provider 之间存在差异。一般来说,使用
IQueryable
时,您不能使用任何方法,除了您的提供程序理解的方法。您可以做的是从数据库中检索数据,然后使用 LINQ to object 进行格式化:
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:
The
AsEnumerable()
extension method forces processing using LINQ to objects.如果我没有解释你的问题,请原谅我。看来您想要做的就是抽象您的 select 方法以供重用。如果是这种情况,您可以考虑使用 lambda 表达式进行投影。例如:
现在您可以这样选择数据集:
至于串联逻辑,选择具有
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:
Now you can select your datasets as such:
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?