Skip to content

Commit

Permalink
Fixing problem with "new Guid()" inside LINQ expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-KaiNet committed Nov 16, 2021
1 parent 621061f commit 7454eec
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/sdk/PnP.Core.Test/SharePoint/ListViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using PnP.Core.Model.SharePoint;
using PnP.Core.QueryModel;
using PnP.Core.Test.Utilities;
using System;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -267,5 +268,21 @@ public async Task AddListViewType2CompactList()
}
}

[TestMethod]
public async Task GetViewByNewGuidTest()
{
//TestCommon.Instance.Mocking = false;

using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
{
var list = context.Web.Lists.GetByServerRelativeUrl($"{context.Uri.LocalPath}/Shared Documents", p => p.Views);
var requestedView = list.Views.AsRequested().First();

var idString = requestedView.Id.ToString();
var view = list.Views.FirstOrDefault(v => v.Id == new Guid(idString));

Assert.IsTrue(view != null);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/sdk/PnP.Core/QueryModel/Query/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ internal static Expression<Func<T, object>>[] CastExpressions<T>(this LambdaExpr
internal static object GetConstantValue(this Expression expression)
{
expression = StripQuotes(expression);
var notSupportedException = new NotSupportedException(string.Format(PnPCoreResources.Exception_Unsupported_ExpressionConstantOnlyTypes, expression, typeof(ConstantExpression), typeof(MemberExpression)));

switch (expression)
{
case ConstantExpression ce:
return ce.Value;
case MemberExpression me:
object obj = GetConstantValue(me.Expression);
return me.Member.GetValue(obj);
case NewExpression ne:
if (ne.Type.Name == nameof(Guid))
{
return Expression.Lambda<Func<Guid>>(expression).Compile().Invoke();
}

throw notSupportedException;
}

throw new NotSupportedException(string.Format(PnPCoreResources.Exception_Unsupported_ExpressionConstantOnlyTypes, expression, typeof(ConstantExpression), typeof(MemberExpression)));
throw notSupportedException;
}

internal static Expression StripQuotes(this Expression e)
Expand Down

0 comments on commit 7454eec

Please sign in to comment.