How can I link one table with two reference tables in LINQ? - c#

I have the following:
IEnumerable<Job> jobs
jobs = from t in table.GetAll()
select new Job
{
Key1 = t.Key1,
Key2 = t.Key2,
Title = t.Title,
Status = t.Status,
Type = t.Type
};
The Status and Type fields are keys and the value of these keys is stored in the following:
refStatus = from t in ref.GetAll()
select new Ref
{
Key1 = "Status"
Key2 = t.Key2, // key that links to Status in the Jobs
Title = t.Title // Text value of the key
}
refType = from t in ref.GetAll()
select new Ref
{
Key1 = "Type"
Key2 = t.Key2, // key that links to Type in the Jobs
Title = t.Title // Text value of the key
}
Is there a way in LINQ that I can link the refStatus and refType tables to the first table and then have my jobs report show the text value of the Status and the Type instead of the key only?
public abstract class Job
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Title { get; set; }
public string Status { get; set; }
public string Type { get; set; }
}
public abstract class Ref
{
public string Key1 { get; set; }
public string Key2 { get; set; }
public string Title { get; set; }
}

Try the following:
var results =
from j in table.GetAll()
join s in refTableStatuses.GetAll() on j.Status equals s.Key2
join t in refTableTypes.GetAll() on j.Type equals t.Key2
select new Job
{
Key1 = j.Key1,
Key2 = j.Key2,
Title = j.Title,
Status = s.Title, // value from Ref (Status) s
Type = t.Title // value from Ref (Type) t
};
You probably need to somehow distinguish between status and type (as they come from same table and are represented by same entity). Since you didn't explain how you differentiate between them, I updated my query variables to reflect that.

Related

Combine two different types into one linq query and sort it

I have two database tables and I'm attempting to create a union query from them. They have different structures:
public partial class Notes
{
public int ID { get; set; }
public int VisitID { get; set; }
public string Note { get; set; }
public DateTime PostDate { get; set; }
public decimal AcctBalance {get; set; }
}
public partial class SystemNotes
{
public int ID {get; set;}
public int VisitID {get; set;}
public int FacilityID {get; set;}
public string Note {get; set;
public DateTime NoteDate {get ;set; }
}
What I want to do is end up with a list of all the data in Notes format sorted by PostDate. What I've tried so far is this:
List<Notes> requests = new List<Notes>();
requests = _context.Notes.Where(i => i.VisitID == VisitID && i.isActive == true).ToList();
List<SystemNotes> requests_s = new List<SystemNotes>();
requests_s = _context.SystemNotes.Where(i => i.VisitID == VisitID).ToList();
var unionA = from a in requests
select new
{
a.ID,
a.VisitID,
a.Note,
a.PostDate,
a.AcctBalance
};
var unionB = from b in requests_s
select new Notes()
{
ID = b.ID,
VisitID = (int)b.VisitID,
Note = b.Note,
PostDate = b.NoteDate,
AcctBalance = (decimal)0.00
};
List<Object> allS = (from x in unionA select (Object)x).ToList();
allS.AddRange((from x in unionB select (Object)x).ToList());
However, PostDate is no longer recognized as an element inside the Object so I can't sort on it. Also, it's in Object format not in Notes format which is what I want for where I'm sending my data. I'm stuck on this one point. Can you assist? Or am I doing this the wrong way in general?
If I correctly understand what you want:
List<Notes> myNotes = new List<Notes> {
new Notes () {
ID = 1,
VisitID = 2
}
};
List<SystemNotes> mySystemNotes = new List<SystemNotes> {
new SystemNotes () {
ID = 3,
VisitID = 4
}
};
var result = myNotes.Select (mn => new { mn.ID, mn.VisitID })
.Union(mySystemNotes.Select (msn => new { msn.ID, msn.VisitID }))
.OrderByDescending(a=>a.ID);
foreach (var currentItem in result)
{
Console.WriteLine ("ID={0}; VisitID={1}", currentItem.ID, currentItem.VisitID);
}

How to cast a dynamic object to a type and compare

I have a model called products,
public class Product
{
public string Name { get; set; }
public string Sku { get; set; }
public float Cost { get; set; }
public string Description { get; set; }
}
I will get a list of objects as type dynamic from externel source as follows
string query = "SELECT Id,Name,KNDYCPQ__Description__c,KNDYCPQ__ProductCode__c FROM KNDY4__Product__c";
List<dynamic> products = new List<dynamic>();
products = await SalesforceSyncProvider.QueryAsync(query);
foreach (var x in products)
{
Product product = new Product();
if (db.Products.Where(a => a.Sku ==x.KNDYCPQ__ProductCode__c).FirstOrDefault() != null) {
product.Name = x.Name;
product.Sku = x.KNDYCPQ__ProductCode__c;
product.Description = x.KNDYCPQ__Description__c;
product.Cost = 2000;
db.Products.Add(product);
}
}
So, before adding to my db, I want to check the product code (KNDYCPQ__ProductCode__c) with my Sku for avoiding duplicates but the if condition is failing and showing the error that, an expression tree may not contain a dynamic operation.
Put x.KNDYCPQ__ProductCode__c in a strongly typed variable and use that in the expression.
string sku = x.KNDYCPQ__ProductCode__c;
if (db.Products.Where(a => a.Sku == sku).FirstOrDefault() != null) {
//...

How can I find matching values from two tables?

I have a question about how to find collections of values from one table that match some values from another table.
Here's my code snippet:
public async Task<List<TableB>> GetTableBResults(string vCode, string number)
{
var tableARepo = DependencyResolver.Get<IRepository<DBTableA>>();
var TableBRepo = DependencyResolver.Get<IRepository<DBTableB>>();
var tableAQuery = string.Format("SELECT * FROM DBTableA WHERE Identifier = '{0}'",
number);
List<DBTableA> tableA = await tableARepo.QueryAsync(tableAQuery);
if (tableA != null)
{
//Find all tableB records with info from Identifier
//And then do a distinct on BusinessName and return those results
foreach (var item in DBTableA)
{
var TableBQuery = String.Format("SELECT *" +
"FROM[DBTableB] INNER JOIN DBTableA" +
"ON DBTableB.Code = {0}" +
"AND DBTableB.HouseNo = {1}" +
"AND DBTableB.BusinessName = {2}" +
"AND DBTableB.VCode = {3}",
item.Code, item.HouseNo, item.FirstName, vCode);
List<DBTableB> tableB = await TableBRepo.QueryAsync(TableBQuery);
if (tableBs != null)
{
return tableBs.Select(_ => new TableB
{
BoroCode = _.BoroCode,
Code = _.Code,
HouseNo = _.HouseNo,
Date = _.Date,
BusinessName = _.BusinessName,
}).ToList();
}
else
{
return new List<TableB>();
}
}
}
return new List<TableB>();
}
Here are the entities:
public class DBTableA
{
[PrimaryKey, AutoIncrement]
public int DBTableAKey { get; set; }
[NotNull]
[Indexed]
public Int64 Identifier { get; set; }
[NotNull]
public int Code { get; set; }
[Indexed]
public int? HouseNo { get; set; }
public string FirstName { get; set; }
}
public class DBTableB
{
[PrimaryKey, AutoIncrement]
public int DBTableBKey { get; set; }
[NotNull]
[Indexed]
public string BoroCode { get; set; }
[NotNull]
[Indexed]
public int Code { get; set; }
[NotNull]
[Indexed]
public string HouseNo { get; set; }
[NotNull]
public DateTime Date { get; set; }
[NotNull]
public string BusinessName { get; set; }
[NotNull]
[Indexed]
public string VCode { get; set; }
}
Basically, using Identifier from TableA, I want to get all matches rows from Table A where Identifier is equal to the number passed in. Then, do a compare where select fields from that tableA set match same values in TableB set
but I'm not sure how to set up the logic, I added my attempt above.
I mean I just want to return the values from Table B that matches the parameters I want to check/match in the sql query above if any. How can I improve my code above to make return the correct values?
EDIT:
Here is some mock data:
Identifier: 123 (only in TableA: grab Code, HouseNo, FirstName that have Identifier 123)
Then match those from Table A with values from Table B where the values are the same like the sample values below:
Code = 456
HouseNo = 34
BusinessName = 'Bar, Foo'
VCode = 'E4T' (passed in to method)
Return TableB row(s) that match the above info.
You should just be able to use LINQ to handle this query:
var ans = (from a in tableARepo
where a.Identifier == number
join b in tableBRepo on new { a.Code, HouseNo = a.HouseNo.ToString(), a.FirstName, VCode = vCode } equals new { b.Code, b.HouseNo, FirstName = b.BusinessName, b.VCode }
select b).ToList();
return ans;

Expression-tree to build sub-select results

I'm trying to build a sub-query by using expression-trees. In linq I would write something like:
var single = MyTable
.AsExpandable()
.Select(c => new
{
Childs = Enumerable.Select(
MyTable.VisibleChilds.Invoke(c, dbContext),
cc => Convert(cfg.ChildsConfig).Invoke(dbContext, cc))
});
where the Convert is building an expression like
p => new MyTableSelect {
Id = p.Id,
Name = p.Name
}
depending on the given values from the config (to only read needed data from database).
but I'm struggeling with the second parameter to be passed to the Select call as I need cc to be passed to the Convert-call.
I guess I need something like Expression.Lambda<Func<>> but I don't see it.
Expression.Lambda>(Expression.Invoke(Instance.Convert(cfg.ChildOrganizersFilterConfig), ContextParameter, theEntity));
I am not familiar with your use of Invoke but if you just want to run a 'Converter' in a fluent syntax for use in a Linq Expression I could show you an example of that. Say I have three POCO classes, one parent container, a child container, and a container I want to convert to.
public class POC
{
public int Id { get; set; }
public string Name { get; set; }
public POC(int id, string name)
{
Id = id;
Name = name;
}
}
public class ChildPOC
{
public int ParentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ChildPOC(int parentId, string firstName, string lastName)
{
ParentId = parentId;
FirstName = firstName;
LastName = lastName;
}
}
public class ChildPOCAlter
{
public int ParentId { get; set; }
public string Name { get; set; }
public ChildPOCAlter(string first, string last, int parentId)
{
ParentId = parentId;
Name = $"{first} {last}";
}
}
I could write a converter method to take ChildPOC to ChildPOCAlter like so:
public static Converter<ChildPOC, ChildPOCAlter> ChildPOCOAlter()
{
return new Converter<ChildPOC, ChildPOCAlter>((x) => { return new ChildPOCAlter(x.FirstName, x.LastName, x.ParentId); });
}
I could then populate some data:
var someParents = new List<POC> { new POC(1, "A"), new POC(2, "B") };
var somechildren = new List<ChildPOC> { new ChildPOC(1, "Brett", "x"), new ChildPOC(1, "Emily", "X"), new ChildPOC(2, "John", "Y") };
And then I may want to take these relationships and apply a converter directly on it:
var relationships = someParents.Select(x => new
{
Id = x.Id,
Name = x.Name,
Children = somechildren.Where(y => y.ParentId == x.Id).ToList().ConvertAll(ChildPOCOAlter())
});

Using Linq to read from multiple tables

I'm sure someone else has asked this but I searched on what I could think of to find the solution.
I've got the following data models to match tables in my SQL db:
public class ProfileDetailModel
{
public string id { get; set; }
public string name { get; set; }
public StyleList[] styleList { get; set; }
public FabricList[] fabricList { get; set; }
}
public class StyleList
{
public string id { get; set; }
public string name { get; set; }
}
public class FabricList
{
public string id { get; set; }
public string fabricName { get; set; }
}
This is the current query code:
var query = (from t in db.tblProfiles
select new ProfileDetailModel()
{
id = t.id,
name = t.name
});
var querylist = await query.ToListAsync();
(prototyped linq queries below for style and fabric)
var styleQuery = (from t in db.tblStyles
select new styleList()
{
id = t.id,
name = t.name
});
var fabricQuery = (from t in db.tblFabrics
select new fabricList()
{
id = t.id,
name = t.name
});
if (queryList.Count > 0)
{
var item = queryList[0];
item.styleList = styleQuery;
item.fabricList = fabricQuery;
}
I'll have one profileDetailModel with multiple items in styleList and in fabricList. EG.
ProfileDetailModel
Data: Pants
styleList: Bell Bottom, Straight Leg, Boot fit
fabricList: jean-blue, jean-black, plaid
All three above models are tables in my db. I could issue 3 separate queries to read the data then assemble after the fact. But is there a way I can do a linq query to include the two arrays in the main query in one shot?
Try this:
var newQuery = (from p in db.tblProfiles
select p)
.AsEnumerable()
.Select(x => new ProfileDetailModel()
{
id = x.id,
name = x.name,
styleList = styleQuery,
fabricList = fabricQuery
});

Categories

Resources