I have table in a database that stores the following objects:
public class MyObjInfoWebView
{
public string SerialNumber { get; set; }
public string ProductCode { get; set; }
public string Description { get; set; }
public string Certificate { get; set; }
public string Language { get; set; }
}
I also have a list of serial numbers, for example this:
var list = new List<string> {"010719/522", "010719/523", "010719/524", "010719/525", "010719/526"}
Is it possible to write (and how) LINQ query to filter objects from the table according to the available list of serial numbers and the required language?
This request solved my problem
_contex.InfoWeb.Where(x => list.Contains(x.SerialNumber) && x.Language == lang).ToListAsync();
Related
I hope this isn't a foolishly simple question. Im very simply trying to figure out how to manipulate a relatively simple table in SQLite through C#.
Im looking to take a parameter and search a List of Arrays for one such array where the parameter matches, and return a related variable within that same array.
For example where an array in the list might be.
Name IATA
Brisbane BNE
The sqlbind:
public static List<Airport> LoadAirports()
{
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
var output = cnn.Query<Airport>("select * from Airport", new DynamicParameters());
return output.ToList();
}
}
The Class:
class Airport
{
int Id { get; set; }
string Name { get; set; }
string LocationName { get; set; }
string IATA { get; set; }
string PortType { get; set; }
string PortOwner { get; set; }
string MotherPort { get; set; }
bool Active { get; set; }
bool IsApplyMeetAndGreet { get; set; }
decimal MeetAndGreet { get; set; }
}
The main Program:
List<Airport> Airports = new List<Airport>();
public FreightCalculator()
{
LoadAirportsList();
string OriginName = OriginInput.Value;
var OriginAirport = Airports.Where(s => s.Name == OriginName);
}
private void LoadAirportsList()
{
Airports = SqliteDataAccess.LoadAirports();
}
Ive tried various combinations of Where, Equals, For each indexing etc. Always getting an error of some kind.
The Error with the above Airports.Where is that the s.Name is inaccessible due to its protection level.
If I do:
var OriginAirport = Airports.Where(Name => Name == OriginName);
I get an error where the operand == cannot be used with Airport and String (Though Name is a string in Airport.)
Im either missing something simple or making this more complicated than it needs to be. Once I find the matching Airport, I need to return the IATA code.
Which I envisage looking like this:
var OriginIATA = OriginAirport.IATA;
Im tired and feeling dumb. Please help :(
Since you declared all members of the Airport class as properties I assume you wanted to expose them publicly.
The error you get is because they are private members and can't be accessed outside the class.
Change "Airport" class to:
class Airport
{
public int Id { get; set; }
public string Name { get; set; }
public string LocationName { get; set; }
public string IATA { get; set; }
public string PortType { get; set; }
public string PortOwner { get; set; }
public string MotherPort { get; set; }
public bool Active { get; set; }
public bool IsApplyMeetAndGreet { get; set; }
public decimal MeetAndGreet { get; set; }
}
I have to find in dictionary in a mongo database.
class Person
{
public string Name { get; set; }
public Dictionary<int,string> AgePersons { get; set; }
}
I have tried two options:
_database.Find(v => v.Name == "test" && v.AgePersons.Any(el => el.Key == 12 && el.Value == "person_name")).ToList();
I got the error: The expression tree is not supported: {document}{dc}'
_database.Find(v => v.Name == "test" && v.AgePersons[12] == "person_name"]).ToList();
I got the error: 'v.AgePersons.get_Item(12) is not supported.'
How can I solve?
A simple option would be to store your objects as:
public class Document
{
public String Name { get; set; }
public List<Item> People { get; set; }
}
public class Item
{
public String Key { get; set; }
public String Value { get; set; }
}
Then map your data on read/write between your classes and these.
A perhaps better option would be to reshape your data when storing into:
public class Item
{
public String Name { get; set; }
public String Key { get; set; }
public String Value { get; set; }
}
Then searching for a Name by Key and Value will be a lot easier and you can still collect the Items into a single object by querying by name or by aggregating and using groupBy name as the last step in the pipeline.
This would also enabling simpler adding or removing of single items without altering large documents.
I have list of objects "orginalobject" is like below and also figure . This is populated from database
orginalobject.monitorName
orginalobject.ProcessGUID
orginalobject.Apikey
orginalobject.AIRSTATION
orginalobject.variableName
orginalobject.id
orginalobject.AIRSTATIONChannel
As in Attached image you can see some columns are repeated that is
monitorName
ProcessGUID
Apikey
AIRSTATION
And non repeatable columns are
variableName
id
AIRSTATIONChannel
so I want list should be grouped by these repeating columns and other columns should become list of this object using lamda or linq
object.monitorName
object.ProcessGUID
object.Apikey
object.AIRSTATION
Object.List<Subobjects> list
And Subobjects class will be like
ObjectSubobjects.variableName
ObjectSubobjects.id
ObjectSubobjects.AIRSTATIONChannel
If I understand you question correctly, you just want to group list of objects by their non-unique monitorName, ProcessGUID, Apikey and AIRSTATION properties used as a key and for each of these groups create list of sub-objects with the remaining unique properties?
This can be done with the following simple linq query. First it uses GroupBy to create groups of originalobjects with the same values of non-unique properties. I used ValueTuple struct to represent combination of non-unique properties as a single object used as a key for grouping. But from provided image it seems that it might be superfluous and that any of the non-unique properties alone used as a grouping key should be sufficient. The rest of the query contains just two projections (Select) that copies values from originalobjects to newly created GroupObjects and ObjectSubobjects.
var groupedObjects = objects.GroupBy(o => (o.monitorName, o.ProcessGUID, o.Apikey, o.AIRSTATION))
.Select(g => new GroupObject()
{
monitorName = g.Key.monitorName,
ProcessGUID = g.Key.ProcessGUID,
Apikey = g.Key.Apikey,
AIRSTATION = g.Key.AIRSTATION,
list = g.Select(s => new ObjectSubobjects()
{
variableName = s.variableName,
id = s.id,
AIRSTATIONChannel = s.AIRSTATIONChannel
})
.ToList()
})
.ToList();
public class orginalobject
{
public string monitorName { get; set; }
public Guid ProcessGUID { get; set; }
public Guid Apikey { get; set; }
public string AIRSTATION { get; set; }
public string variableName { get; set; }
public Guid id { get; set; }
public int AIRSTATIONChannel { get; set; }
}
public class GroupObject
{
public string monitorName { get; set; }
public Guid ProcessGUID { get; set; }
public Guid Apikey { get; set; }
public string AIRSTATION { get; set; }
public List<ObjectSubobjects> list { get; set; }
}
public class ObjectSubobjects
{
public string variableName { get; set; }
public Guid id { get; set; }
public int AIRSTATIONChannel { get; set; }
}
I am finding this very hard to understand and where to start, so I was hoping that some one would be able to point in the correct direction. I have a list(customers) inside which there are arrays/lists. Basically I want to flatten all the results of the list into a flat version if the list.
public class Customer : EntityBase
{
public Phonenumber[] PhoneNumbers { get; set; }
public Contact BillToContact { get; set; }
public Terms Terms { get; set; }
}
public class Contact
{
public Phonenumber[] PhoneNumbers { get; set; }
public Address Address { get; set; }
public Key Key { get; set; }
public string CompanyName { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
}
public class Phonenumber
{
public string Number { get; set; }
public int Key { get; set; }
}
public class Terms
{
public int DueDays { get; set; }
public int DiscountDays { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
public abstract class EntityBase
{
public Guid Id { get; set; }
public string Status { get; set; }
public int Rev { get; set; }
}
I have tried many approaches and just keep getting more confused. So if anyone could help or even point me in the right direction I would be extremely grateful. below is one of the approaches I have tried.
public IEnumerable<Customer> Find (Func<Customer , bool> predicate) {
foreach (var p in Customer.SelectMany(p => p)) {
if(predicate(p)) {
yield return p;
}
}
}
I am Deserializing a jason string into a list but then want to display in a datagrid, but igGrid does not support binding to nested(complex) properties. So I need to flatten the list so that there is no sub levels of the list.
To select an array of PhoneNumber from List<Customer> use SelectMany:
List<Customer> customers = [data];
PhoneNumber phoneNumbers = customers.SelectMany(x=>x.PhoneNumbers).ToArray();
It's not clear at all from your question what output you actually want. Do you just want a list of all the phone numbers? Or do you want to preserve the other Customer information, such that you get multiple instances of the Customer information, each instance with a separate phone number?
You can accomplish the former with something like this:
IEnumerable<Phonenumber> numbers =
customers.SelectMany(
customer => customer.PhoneNumbers
.Concat(BillToContact.PhoneNumbers));
If you only want the Customer.PhoneNumbers numbers and not those in the BillToContact object, just leave the .Concat(BillToContact.PhoneNumbers) out of the above.
If you want to preserve one or more values from the original Customer object, you can do something like this:
var numbers = customers.SelectMany(
customer => customer.PhoneNumbers.Select(
number => new
{
Number = number,
FirstName = customer.BillToContact.FirstName,
Email = customer.BillToContact.Email
}));
The above will generate an enumeration of anonymous type objects, each having a single phone number, along with the corresponding FirstName and Email values from the associated Contact object. You can of course mix and match (e.g. use .Concat(...) to include phone numbers from the BillToContact object), and include whichever specific Customer or Contact members you want.
I have two different document collections in my RavenDB database - Teams and Matches. The documents look like this:
public class Team {
public string Id { get; set; }
public string Name { get; set; }
public int LeaguePosition { get; set; }
}
public class Match {
public string Id { get; set; }
public string HomeTeamName { get; set; }
public string AwayTeamName { get; set; }
public DateTime StartTime { get; set; }
}
So basically I have teams and matches between these teams. However, for certain operations I need to get an entity which look something like the following from the database:
public class MatchWithExtraData {
public string Id { get; set; } // Id from the match document.
public string HomeTeamId { get; set; }
public string HomeTeamName { get; set; }
public int HomeTeamPosition { get; set; }
public string AwayTeamId { get; set; }
public string AwayTeamName { get; set; }
public int AwayTeamPosition { get; set; }
public DateTime? StartTime { get; set; }
}
What I want is really the match document but with extra fields for the home and away teams' ids and league positions. Basically join the match document on home and away team name with two team documents, one for the home team and one for the away team. I figured that a multi map/reduce index should do the trick so I have started with the following index:
public class MatchWithExtraDataIndex: AbstractMultiMapIndexCreationTask<MatchWithExtraData> {
public MatchWithExtraData() {
AddMap<Team>(
teams => from team in teams
select new {
Id = (string)null,
HomeTeamId = team.Id,
HomeTeamName = team.Name,
HomeTeamPosition = team.LeaguePosition,
AwayTeamId = team.Id,
AwayTeamName = team.Name,
AwayTeamPosition = team.LeaguePosition,
StartTime = (DateTime?)null
}
);
AddMap<Match>(
matches => from match in matches
select new {
Id = match.Id,
HomeTeamId = (string)null,
HomeTeamName = match.HomeTeamName,
HomeTeamPosition = 0,
AwayTeamId = (string)null,
AwayTeamName = match.AwayTeamName,
AwayTeamPosition = 0,
StartTime = match.StartTime
}
);
Reduce = results => from result in results
// NOW WHAT?
}
}
The reduce part is the one I can't figure out since there are two teams in each match. I think I need to do a nested group by, first on the HomeTeamName, and then on the AwayTeamName but I can't figure out how to do that.
Maybe this is more a LINQ problem than a RavenDB problem. But how would such a nested group by statement look? Or could it be done in another way?
You are better off using Transform Results for that, or includes.
See the docs here: http://ravendb.net/docs/client-api/querying/handling-document-relationships