LINQ: Typing my first ever LINQ query and the system is not recognizing my code line which is as follows:
int[] i= { 1, 2, 3, 4, 5 };
IEnumerable<int> j = from r in i select r;
My "i" bears red squiggly saying - a field initializer cannot reference the nonstatic field method or property
A field initializer cannot reference the nonstatic field method or
property
This error means you're trying to initialize your IEnumerable<T> inside a class level declaration with a LINQ query. If you want to initialize that field, do so inside the class constructor:
public class SomeClass
{
int[] I = { 1, 2, 3, 4, 5 };
IEnumerable<int> J { get; set; }
public SomeClass()
{
J = from r in i select r;
}
}
Related
If I have a class like this
`
class Person
{
public string First;
public string Last;
public bool IsMarried;
public int Age;
}`
Then how can I write a LINQ Expression where I could select properties of a Person. I want to do something like this (user can enter 1..n properties)
SelectData<Person>(x=>x.First, x.Last,x.Age);
What would be the input expression of my SelectData function ?
SelectData(Expression<Func<TEntity, List<string>>> selector); ?
EDIT
In my SelectData function I want to extract property names and then generate SELECT clause of my SQL Query dynamically.
SOLUTION
Ok, so what I have done is to have my SelectData as
public IEnumerable<TEntity> SelectData(Expression<Func<TEntity, object>> expression)
{
NewExpression body = (NewExpression)expression.Body;
List<string> columns = new List<string>();
foreach(var arg in body.Arguments)
{
var exp = (MemberExpression)arg;
columns.Add(exp.Member.Name);
}
//build query
And to use it I call it like this
ccc<Person>().SelectData(x => new { x.First, x.Last, x.Age });
Hopefully it would help someone who is looking :)
Thanks,
IY
I think it would be better to use delegates instead of Reflection. Apart from the fact that delegates will be faster, the compiler will complain if you try to fetch property values that do not exist. With reflection you won't find errors until run time.
Luckily there is already something like that. it is implemented as an extension function of IEnumerable, and it is called Select (irony intended)
I think you want something like this:
I have a sequence of Persons, and I want you to create a Linq
statement that returns per Person a new object that contains the
properties First and Last.
Or:
I have a sequence of Persns and I want you to create a Linq statement
that returns per Person a new object that contains Age, IsMarried,
whether it is an adult and to make it difficult: one Property called
Name which is a combination of First and Last
The function SelectData would be something like this:
IEnumerable<TResult> SelectData<TSource, TResult>(this IEnumerable<TSource> source,
Func<TSource, TResult> selector)
{
return source.Select(selector);
}
Usage:
problem 1: return per Person a new object that contains the
properties First and Last.
var result = Persons.SelectData(person => new
{
First = person.First,
Last = person.Last,
});
problem 2: return per Person a new object that contains Age, IsMarried, whether he is an adult and one Property called Name which is a combination
of First and Last
var result = Persons.SelectData(person => new
{
Age = person.Name,
IsMarried = person.IsMarried,
IsAdult = person.Age > 21,
Name = new
{
First = person.First,
Last = person.Last,
},
});
Well let's face it, your SelectData is nothing more than Enumerable.Select
You could of course create a function where you'd let the caller provide a list of properties he wants, but (1) that would limit his possibilities to design the end result and (2) it would be way more typing for him to call the function.
Instead of:
.Select(p => new
{
P1 = p.Property1,
P2 = p.Property2,
}
he would have to type something like
.SelectData(new List<Func<TSource, TResult>()
{
p => p.Property1, // first element of the property list
p -> p.Property2, // second element of the property list
}
You won't be able to name the returned properties, you won't be able to combine several properties into one:
.Select(p => p.First + p.Last)
And what would you gain by it?
Highly discouraged requirement!
You could achive similar result using Reflection and Extension Method
Model:
namespace ConsoleApplication2
{
class Person
{
public string First { get; set; }
public string Last { get; set; }
public bool IsMarried { get; set; }
public int Age { get; set; }
}
}
Service:
using System.Collections.Generic;
using System.Linq;
namespace Test
{
public static class Service
{
public static IQueryable<IQueryable<KeyValuePair<string, object>>> SelectData<T>(this IQueryable<T> queryable, string[] properties)
{
var queryResult = new List<IQueryable<KeyValuePair<string, object>>>();
foreach (T entity in queryable)
{
var entityProperties = new List<KeyValuePair<string, object>>();
foreach (string property in properties)
{
var value = typeof(T).GetProperty(property).GetValue(entity);
var entityProperty = new KeyValuePair<string, object>(property, value);
entityProperties.Add(entityProperty);
}
queryResult.Add(entityProperties.AsQueryable());
}
return queryResult.AsQueryable();
}
}
}
Usage:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var list = new List<Person>()
{
new Person()
{
Age = 18,
First = "test1",
IsMarried = false,
Last = "test2"
},
new Person()
{
Age = 40,
First = "test3",
IsMarried = true,
Last = "test4"
}
};
var queryableList = list.AsQueryable();
string[] properties = { "Age", "Last" };
var result = queryableList.SelectData(properties);
foreach (var element in result)
{
foreach (var property in element)
{
Console.WriteLine($"{property.Key}: {property.Value}");
}
}
Console.ReadKey();
}
}
}
Result:
Age: 18
Last: test2
Age: 40
Last: test4
i am trying a linq query to sort a group of elements and then bring a elements that satisfy particular condition to top.
for eg, if i have list of elements like below:
ID Names
1 Angie
2 Bret
3 Salva
4 cunnighma
5 maria
6 Galvin
7 Newton
8 Desmond
and if i pass condition as Name=Galvin then the resultset should be sorted first and then bring the value inn condition to top.The resultset would like below
ID Names
6 Galvin
1 Angie
2 Bret
4 cunnighma
8 Desmond
5 maria
7 Newton
3 Salva
One option is to create an extension method that can be used in your linq expression. The following solution does just that. I've made it work for the case of multiple matches as well.
The example code below will have the following output:
6:Galvin
1:Angie
2:Bret
4:cunnighma
8:Desmond
5:maria
7:Newton
3:Salva
Here's the code:
void Main()
{
// Setup the example data
var names = new List<Record>
{
new Record { Id = 1, Name = "Angie" },
new Record { Id = 2, Name = "Bret" },
new Record { Id = 3, Name = "Salva" },
new Record { Id = 4, Name = "cunnighma" },
new Record { Id = 5, Name = "maria" },
new Record { Id = 6, Name = "Galvin" },
new Record { Id = 7, Name = "Newton" },
new Record { Id = 8, Name = "Desmond" }
};
// Sort the list and move the matches to the top
var result = names
.OrderBy(x => x.Name)
.MoveToTop(x => x.Name.Contains("alvin"));
// Display the results to the console
result
.ToList()
.ForEach(x => Console.WriteLine($"{x.Id}:{x.Name}"));
}
public class Record
{
public int Id { get; set; }
public string Name { get; set; }
}
public static class EnumerableExtensions
{
public static IEnumerable<T> MoveToTop<T>(this IEnumerable<T> list, Func<T, bool> predicate)
{
var matches = list.Where(predicate);
return matches.Concat(list.Except(matches));
}
}
I create this class for a test.
I want to compare to List of the class and get different class between ListA and ListB. In my example the result get only class of ListB.
I do the same thin with list of string and work it
Class Example
public class FileNode
{
public string Source { get; set; }
public int Id { get; set; }
}
List<FileNode> ListA = new List<FileNode>
{
new FileNode{ Id = 1, Source="a" },
new FileNode{ Id = 2, Source="b" },
};
List<FileNode> ListB = new List<FileNode>
{
new FileNode{ Id = 1, Source="a" },
new FileNode{ Id = 2, Source="b" },
new FileNode{ Id = 3, Source="c" },
};
List<FileNode> ListAB = ListB.Where(m => !ListA.Contains(m)).ToList();
String example, it's works
List<string> a = new List<string> {"a","b","c","d","e" };
List<string> b = new List<string> {"a","b","c","d" };
List<string> ab = a.Where(m => !b.Contains(m)).ToList();
Well Contains is going to call Equals on the elements - and may also use GetHashCode (I doubt it, but you should override it consistently anyway). So you need to override Equals(object) and GetHashCode() in FileNode. (By default, you'll get reference equality.)
Note that as soon as you start trying to use Contains in a query which will execute in the database, it could behave completely different - it wouldn't be looking at your Equals/GetHashCode methods at that point.
I need to pass in a "where" lambda expression that'll be used in a LINQ query inside a method. The problem is, I don't know what the where value will be compared against until I get into the method.
Now to explain further and clarify some of what I said above I'll come up with a bit of a contrived example.
Imagine I have a List<Products> and I need to narrow that list down to a single record using a productId property of the Products object. Normally I would do this:
var product = productList.Where(p=>p.productId == 123).FirstOrDefault();
Now take it a step further - I need to put the above logic into a method that isn't limited to a List<Products> but is instead a List<T> so ideally, I'd be calling it like this (and I know the below won't work, it's simply here to show what I am trying to achieve):
myMethod(productList, p => p.productId == X)
With the caveat being that X isn't known until I'm inside the method.
Finally, for what it's worth, I need to point out that my collection of data is an OData DataServiceQuery.
So, to re-summarize my question: I need to know how to construct a lambda "where" expression that I can pass into a method and how to use it against a collection of objects in a LINQ query.
myMethod(productList, p => p.productId == X) - you can emulate with this trick
static void myMethod<T>(List<T> list, Func<T,bool> predicate, ref int x)
{
x = 5;
var v = list.Where(predicate);
foreach (var i in v)
Console.Write(i);
Console.ReadLine();
}
static void Main(string[] args)
{
List<int> x = new List<int> { 1, 2, 3, 4, 5 };
int z = 0;
myMethod(x, p => p == z, ref z);
}
but not sure if it solves your problem in whole
For one, if you are going to query an IEnumerable<T>, you will need to ensure that your comparison will work in the first place. In that case you can make your objects implement an interface that guarantees that they will support the comparison.
Once you do that, your method can have a generic constraint that limits the input to those interfaces. At that point, your method can take a Func, which can be passed to the LINQ Where clause:
public interface Identifier
{
int Id { get; set; }
}
public class Product : Identifier
{
public int Id { get; set; }
//Other stuff
}
public T GetMatch<T>(IEnumerable<T> collection, Func<T, int, bool> predicate) where T : Identifier
{
int comparison = 5;
return collection.Where(item => predicate(item, comparison)).FirstOrDefault();
}
Which can be invoked like:
var match = GetMatch<Identifier>(collection, (x, y) => x.Id == y);
UPDATE:
I modified the above code to take in a comparison parameter
You could try to use the PredicateBuilder class from the free LinqKit library(tutorial).
You can then construct a predicate using
PredicateBuilder predicate = PredicateBuilder.True<T>();
predicate = PredicateBuilder.And(predicate, p=> p.product_id == X);
where X is of type T.
You can use this predicate in a where clause such as .Where(predicate) and return an IQueryable or return the predicate itself which would be of type Expression<Func<T, bool>>
I'm not sure how to ask this, but I want to create a new object in a linq statement, and initialize it's values. Since I have to use StructureMap's GetInstance method to get an instance of the object, I don't think this is possible. Am I wrong? And before we get off-topic, I realize I could probably create a constructor that takes the values I want to use, but I want to know if there's any way that I can do it with initializers and structuremap. I'm really weak on structuremap, and for all I know, there may be a simple way I'm just missing...
Thanks for any thoughts.
//basically, I'm creating a new object when I don't have an existing one for the groupid
//conceptually, this is adding users to the selected groups if they don't already belong
from g in GroupIDs where !OriginalGroups.Exists(ug => ug.SecurityGroupID == g) select StructureMap.ObjectFactory.GetInstance() {//initialize values of usertosecuritygroup};
//this is how I would do it if I wasn't relying on DI.
from g in GroupIDs where !OriginalGroups.Exists(ug => ug.SecurityGroupID == g) select new UserToGroup {UserID = UserID, GroupID = g};
If you start using method chains rather than LINQ expressions you could do something like this:
[TestFixture]
public class so_1
{
public class UserToGroup
{
public int UserId { get; set; }
public int GroupId { get; set; }
}
[Test]
public void TEST()
{
var container = new Container();
var groupIds = new int[] { 1,2,3,4,5};
var originaGroups = new int[] { 4, 5 };
var result = groupIds.Intersect(originaGroups).Select(group =>
{
var myClass = container.GetInstance<UserToGroup>();
myClass.GroupId = group;
return myClass;
});
result.First().GroupId.ShouldEqual(4);
result.Skip(1).First().GroupId.ShouldEqual(5);
}
}