第一次接触Linq,求大神指点指点。

zj25810 2014-06-30 04:45:08

string Sqlw = " 1==1 ";
if (typeid > 0)
Sqlw = "&& result => result.Field<int>(\"typeid\")==" + typeid;
if (quest != "")
{
//这一段是我自作主张改的一个非空查询或者模糊查询的方法,
//quest是一个传参。如果为"name"或者"name|"就代表我这个name字段不能为空
//如果quest传过来为"name|123"就代表我要根据"123"来对name字段做模糊查询。
string[] arr = quest.Split('|');
if (arr.Length > 2)
{
if (arr[1] != "")
{
Sqlw = "&& result => result." + arr[0] + ".Contains('" + arr[1] + "')";
}
else
Sqlw = "&& result => result.Field<string>(\"" + arr[0] + "\")!= \"\"";
}
else
Sqlw = "&& result => result.Field<string>(\"" + arr[0] + "\")!= \"\"";
}

var x = from result in dt.AsEnumerable() where Sqlw select result;


现在我已经错得很离谱的就是from result in dt.AsEnumerable() where Sqlw select result这一段的Sqlw 根本不是这么拼的,
但是我Linq基础基本为0,完全不知道这样的问题该怎么解决。还有我这样Linq的写法有错误吗?

也不要跟我说什么不懂就先去看基础资料,我要不是赶时间,我真的就抱着书从头啃了。
...全文
65 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2014-06-30
  • 打赏
  • 举报
回复
手写的代码,提供思路参考:
   var x = from result in dt.AsEnumerable() select result;
    if (typeid > 0)
                x = x.Where( result => result.Field<int>("typeid")== typeid);
            if (quest != "")
            { 
                string[] arr = quest.Split('|');
                if (arr.Length > 2)
                {
                    if (arr[1] != "")
                    {
                        x = x.Where(  result => GetPropertyValue(result, arr[0] ).ToString().Contains( arr[1] +);
                    }
                    else
                        x = x.Where( result =>  GetPropertyValue(result, arr[0] ) != "";
                }
                else
                    x = x.Where( result =>  GetPropertyValue(result, arr[0] ) != "";
            }   



private static object GetPropertyValue(object obj, string property)  
{  
    System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property);  
    return propertyInfo.GetValue(obj, null);  
}     
zj25810 2014-06-30
  • 打赏
  • 举报
回复
引用 6 楼 q107770540 的回复:
我明白你的意思,你的需求,简单点可以通过拼接查询条件来实现: http://blog.csdn.net/q107770540/article/details/5724013
谢谢,谢谢。要的就是这种答案!
q107770540 2014-06-30
  • 打赏
  • 举报
回复
我明白你的意思,你的需求,简单点可以通过拼接查询条件来实现: http://blog.csdn.net/q107770540/article/details/5724013
zj25810 2014-06-30
  • 打赏
  • 举报
回复
引用 1 楼 no200830740203 的回复:
你把linq的lambda表达式跟本身的ef linq写法混淆了 1.
var q = from result in table where result.a == a select result
2.
var q = table.Where(e=>e.a == a).Select()
这个...可能是我参考了几种方案,结果半懂不懂的就搞混了吧。等下纠正
zj25810 2014-06-30
  • 打赏
  • 举报
回复
引用 3 楼 FoxDave 的回复:
sqlw就是查询条件
SqlW是我根据需求拼写的查询条件,现在我不知道怎么插入Linq语句里面去。
Justin-Liu 2014-06-30
  • 打赏
  • 举报
回复
sqlw就是查询条件
  • 打赏
  • 举报
回复
大概就是这个意思,再深奥我也说不清楚,如错请无视
  • 打赏
  • 举报
回复
你把linq的lambda表达式跟本身的ef linq写法混淆了 1.
var q = from result in table where result.a == a select result
2.
var q = table.Where(e=>e.a == a).Select()

8,497

社区成员

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

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