LINQ,where,customer method

thestarmm 2010-07-09 09:33:01

from t in Table1
where Method1(t.Field1)==1
select t


int Method1(string data)
{
//处理data数据,返回int
}

这是我第一次使用linq,是不是不能这样做?
提示....no supported translation to SQL.
如果我想这么做(加工某个字段的值),该怎么做?
...全文
148 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
thestarmm 2010-07-09
  • 打赏
  • 举报
回复

Func<string, int > fn = (data) => int.Parse(data)+60;
var query=from t1 in Table1 where fn(t1.Field1)==100
select t1;
query.Dump();

就用上面的好了
ERROR:NotSupportedException: Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL.

其实我最想知道的是,sql中可以写function,然后存储过程,sql中能使用,
那么在代码中写linq,我能不能使用自己的method来处理数据,因为我不一定是linq to sql,可能还有linq to dataset.

Notes:
1.linq 可以使用sql中的function
2.上面只是个例子,fn(t1.Field1)替换Convert.ToInt32(t1.Field1)+60可以解决的
宝_爸 2010-07-09
  • 打赏
  • 举报
回复
我也没有用过。
不过google到一篇文章,你可以参考下


Calling functions in LINQ queries
wuyq11 2010-07-09
  • 打赏
  • 举报
回复
var query=from c in ds.Tables["Customers"].AsEnumerable()
WHERE 方法(c.Field<string>("CustomerID"))==""
select new{
CustomerID=c.Field<string>("CustomerID")
};

q107770540 2010-07-09
  • 打赏
  • 举报
回复

//将此方法代码帖上来
int Method1(string data)
{
//处理data数据,返回int
}
jiangshun 2010-07-09
  • 打赏
  • 举报
回复
具体怎么处理的帖出来


thestarmm 2010-07-09
  • 打赏
  • 举报
回复
不好意思大家,很忙,回帖不及时.

谢谢10楼wjn161,一下子我突然明白可以转为IEnumerable<T>后再操作就行了,
然后一回顾wuyq11的内容,发现自己也马虎了.....
(不该怪q107770540,jiangshun,其实大家看帖回帖都很快,会漏掉内容的,
findcaiyzh的文章我昨天看过:))

对不起大家了,让大家白白花了时间.答案可以参看wuyq11的回答
刚接触linq,发现很多内容需要学习.有什么书或者文章可以介绍介绍吗
(好像Linq 是非主流一样,MS不在乎它了.)

先留一天,明天结贴,有对Linq有意见或建议的可以发表,谢谢.
thestarmm 2010-07-09
  • 打赏
  • 举报
回复
不好意思,楼上你的是Linq to Object,我例子中处理的是Linq to SQL.
你看下5楼显示的错误:
ERROR:NotSupportedException: Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL.

NOTES
LINQ to Object中可以做到以上内容:

//work
Func<string, int > fn = (data) => int.Parse(data)+60;
var strings=new []{"40","50","60"};
var query1=from t1 in strings where fn(t1)==100 select t1;
query1.Dump();
wjn161 2010-07-09
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
/// <summary>
/// Struct or Class
/// Data Model
/// </summary>
public struct Products
{
public string number;
//..other field;
//..
//..
}
class Program
{
static void Main(string[] args)
{
List<Products> tmpList = new List<Products>();//like your table1
//insert a data to the table
#region Test Data
Products product = new Products();
product.number = "2";
tmpList.Add(product);
#endregion
// this is what you want
var tmpList1 = tmpList.Where(t => Method(t.number).Equals(10002));//Lamada Form
//your Form:
//var tmpList1 = from t in tmpList where Method(t.number) == 10002 select t;

//check the result
foreach(var item in tmpList1)
{
Console.WriteLine(item.number);
}
Console.Read();
}

static int Method(string data)
{
//Your Process
return int.Parse("1000" + data);//Just a Sample
}
}
}

VS2010测试通过~
thestarmm 2010-07-09
  • 打赏
  • 举报
回复
这段代码一看就知道是随便写的,肯定是个用来揭示问题的sample而已
thestarmm 2010-07-09
  • 打赏
  • 举报
回复
不好意思,你们看帖子真的很马虎!请看NOTES
jiangshun 2010-07-09
  • 打赏
  • 举报
回复
Func<string, int > fn = (data) => int.Parse(data)+60;
var query=from t1 in Table1 where fn(t1.Field1)==100
select t1;
query.Dump();



var query=from t1 in Table1 where Convert.ToInt32(t1.Field1)+60==100
select t1;
query.Dump();
q107770540 2010-07-09
  • 打赏
  • 举报
回复
我晕倒



var query=from t1 in Table1 where t1.Field1==40 //这不画蛇添足嘛,是直接写成40不就行了?
select t1;
query.Dump();

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧