相关疑难解决方法(0)

想要一个Query在Linq查询中按变量排序

如何通过Column变量进行排序,因为我在页面上有一个下拉列表,我想根据在此下拉列表中选择的sord顺序显示网格,例如价格,代码,评级,描述等等,我不想为每个单独写一个查询柱.

from lm in lDc.tbl_Products
where lm.TypeRef == pTypeId
 orderby lm.Code ascending
 select new; 
Run Code Online (Sandbox Code Playgroud)

linq linq-to-entities linq-to-sql

5
推荐指数
1
解决办法
5607
查看次数

Linq和订购

我有一个泛型类,可以使用通用的OrderBy参数

课程如下

 class abc<T> where T : myType
 {
   public abc(....., orderBy_Argument ){ ... }
   void someMethod(arg1, arg2, bool afterSort = false)
   {
       IEnumerable<myType> res ;
       if ( afterSort && orderBy_Argument != null )
          res = src.Except(tgt).OrderBy( .... );
       else
          res = src.Except(tgt);
   }
}
Run Code Online (Sandbox Code Playgroud)

orderBy可以是各种类型

例如

.OrderBy( person => person.FirstName )
.OrderBy( person => person.LastName )
.OrderBy( person => person.LastName, caseInsensitive etc )
Run Code Online (Sandbox Code Playgroud)

目标是使orderBy成为一个参数,而不是将其烘焙

有任何想法吗 ?

.net c# linq lambda

5
推荐指数
1
解决办法
299
查看次数

在LINQ中使用PropertyInfo对象查询集合

我有一个像这样的签名的方法

void RefreshMethod<T>(IEnumerable<T> lst, string propertyName) where T:class
{
   Type type = typeof(T);
   PropertyInfo property = type.GetProperties().Single(u => u.Name == primaryKeyProperty);
  //query goes here
}
Run Code Online (Sandbox Code Playgroud)

现在我想查询该集合以获取其所有值

propertyName <0

在一个简单的场景中,它就像这样容易

lst.where(u=>u.ID<0)
Run Code Online (Sandbox Code Playgroud)

但在这里我没有那个ID属性,但有相应的"PropertyInfo"对象.

我应该怎么做到这一点.

善意的指导

c# linq reflection

5
推荐指数
1
解决办法
4017
查看次数

无法使用Int64强制转换System.Func`2类型的对象

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using dynamic = System.Linq.Dynamic;
using System.Linq.Expressions;

namespace Project.Lib.Extensions
{
    public static partial class Utils
    {
        public static List<T> SortForMe<T>(this List<T> list, string propertyName,SortDirection sortDirection)
        {
            string exp1 = string.Format("model.{0}", propertyName);
            var p1 = Expression.Parameter(typeof(T), "model");
            var e1 = dynamic.DynamicExpression.ParseLambda(new[] { p1 }, null, exp1);

            if (e1 != null)
            {
                if (sortDirection==SortDirection.Ascending)
                {
                    var result = list.OrderBy((Func<T, object>)e1.Compile()).ToList();
                    return result;
                }
                else
                {
                    var result …
Run Code Online (Sandbox Code Playgroud)

c# linq reflection dynamic

5
推荐指数
1
解决办法
2072
查看次数

对绑定到自定义通用对象列表的GridView进行排序

我试图找出如何排序GridView具有多个列(String,DateTime,Decimal这势必将自定义对象的泛型列表等数据类型).

MyObject.vb:

Public Property Id
Public Property Name
Public Property Date
Public Property Amount
Run Code Online (Sandbox Code Playgroud)

MyObjects.aspx.vb:

gridView.DataSource = GetMyObjects()
gridView.DataBind()
Run Code Online (Sandbox Code Playgroud)

:GetMyObjects()返回ListMyObject

基本上,我需要能够单击网格的列标题进行排序和反向排序,并且还能够存储排序方向,ViewState以便每次单击列标题时方向都会保留.

好像我可能需要MyObject实现IComparable,但我不确定如何将它们放在一起.

任何人都可以建议一个很好的教程,或指出我正确的方向?

vb.net asp.net sorting gridview

5
推荐指数
1
解决办法
6100
查看次数

动态LINQ OrderBy +方法计数

我正在网页中显示一个对象公司表,我正在使用动态Linq OrderBy对每个属性进行排序.我正在使用此代码/sf/answers/16345381/

public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
    return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
    return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
    return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
    return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName) {
    string[] props = property.Split('.');
    Type type = typeof(T);
    ParameterExpression arg …
Run Code Online (Sandbox Code Playgroud)

c# linq

5
推荐指数
1
解决办法
1737
查看次数

LINQ 如何动态使用多个ThenBy?

我没想到这会如此罕见,但看起来确实如此。情况是这样的。

我有一个ApplySort方法,它接受从 mvc 页面发布的 3 个参数。

    private List<dynamic> ApplySort(List<dynamic> listToBeSorted, string sortBy, string sortOrder)
    {
        if (String.IsNullOrEmpty(sortBy))
            sortBy = "createddate";
        if (String.IsNullOrEmpty(sortOrder) || sortOrder.Trim() != "0")
            sortOrder = "1"; // 1 = descending, 0 = ascending
        if (sortOrder == "1")
        {
            switch (sortBy)
            {
                case "name":
                    listToBeSorted = listToBeSorted.OrderByDescending(a => a.name).ToList();
                    break;
                case "assigned":
                    listToBeSorted = listToBeSorted.OrderByDescending(a => a.title).ToList();
                    break;
                case "duedate":
                    listToBeSorted = listToBeSorted.OrderByDescending(a => a.end).ToList();
                    break;
                case "status":
                    listToBeSorted = listToBeSorted.OrderByDescending(a => a.title).ToList();
                    break;
                default:
                    listToBeSorted …
Run Code Online (Sandbox Code Playgroud)

.net c# linq sorting

5
推荐指数
1
解决办法
4281
查看次数

Linq中的动态订单

我有一个访问数据库的应用程序,必须根据输入按不同的字段排序结果.

这是我的排序功能:

IQueryable<Entity> GetSortedData(IQueryable<Entity> result, String orderby, bool desc)
{
   switch (orderby.ToLower())
   {
     case "id":
       result = result.OrderBy(c => c.Id);
       break;

     case "code":
       result = result.OrderBy(c => c.Code);
       break;

     case "active":
       result = result.OrderBy(c => c.Active);
       break;

     default:
       result = result.OrderBy(c => c.Name);
       break;
   }

   if (pageData.SortDesc)
   {
     var res = result.ToList();
     res.Reverse();
     return res.AsQueryable();
   }

   return result;      
}
Run Code Online (Sandbox Code Playgroud)

这段代码存在一些我不喜欢的问题:

  1. 重复的代码太多了.如果它是"纯粹的SQL"查询,它看起来像

    SELECT*FROM data_table ORDER BY CASE @OrderBy WHEN'id'Then id when'code'THEN code WHEN'active'THEN
    active ELSE name
    END;

  2. 转换为列表并返回以进行反转.我不能改变返回值类型,我绝对不想写更无用的代码(基本上是翻倍switch …

c# linq

5
推荐指数
1
解决办法
2171
查看次数

动态LINQ喜欢

如何为Like子句编写动态linq方法.

作为参考,在IEnumerable <T>上动态LINQ OrderBy.我正在为动态Like子句寻找类似的一个.

我有以下扩展方法:

public static IQueryable<T> Like<T>(this IQueryable<T> source, string propertyName, 
                                    string keyword)
{
    var type = typeof(T);
    var property = type.GetProperty(propertyName);
    var parameter = Expression.Parameter(type, "p");
    var propertyAccess = Expression.MakeMemberAccess(parameter, property);
    var constant = Expression.Constant("%" + keyword + "%");
    var methodExp = Expression.Call(
        null, 
        typeof(SqlMethods).GetMethod("Like", new[] { typeof(string), typeof(string) }),
        propertyAccess, 
        constant);
    var lambda = Expression.Lambda<Func<T, bool>>(methodExp, parameter);
    return source.Where(lambda);
}
Run Code Online (Sandbox Code Playgroud)

上述方法给出了错误

方法'Boolean Like(System.String,System.String)'不能在客户端上使用; 它仅用于转换为SQL.

在IEnumerable <T>上动态LINQ …

linq dynamic

4
推荐指数
1
解决办法
5740
查看次数

如何动态构建()=> x.prop lambda表达式?

我的代码就像

DepartmentPaperConsumption dto = null;
Run Code Online (Sandbox Code Playgroud)

然后我有NHibernate QueryOver结果,我想订购它

result.OrderByAlias(() => dto.TotalColorCopys);
Run Code Online (Sandbox Code Playgroud)

但我希望能够dto用字符串动态指定任何属性.我尝试使用动态LINQ,但似乎我无法得到它.我也试过从头开始构建LambdaExpression - 也没有运气.我将不胜感激任何帮助.

c# linq nhibernate lambda queryover

4
推荐指数
1
解决办法
2万
查看次数