Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1014223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:13:18+00:00 2026-05-16T10:13:18+00:00

I have the below SQL which works just fine: SELECT Message, CreateDate, AccountId, AlertTypeId

  • 0

I have the below SQL which works just fine:

SELECT     Message, CreateDate, AccountId, AlertTypeId
FROM       dbo.Alerts
UNION
SELECT     TOP (100) PERCENT Status, CreateDate, AccountId,
                          (SELECT     10 AS Expr1) AS AlertTypeId
FROM         dbo.StatusUpdates
WHERE AccountId = PassedInParameter
ORDER BY CreateDate DESC

I am trying to convert it to LINQ, which doesn’t work just fine 🙂 Obviously, there is a lot wrong here – it is just a rough start. It does not account for the above temp column or the order by condition and the generics / return type ambiguity is my attempt to make sense of the two different return types:

public List<T> GetSomething<T>(Int32 accountId)
{
   List<T> result;

   using (DataContext dc = _conn.GetContext())
   {
      IEnumerable<Alert> alerts = (from a in dc.Alerts
                                   where a.AccountId == accountId
                                   select a);
      IEnumerable<StatusUpdate> updates = (from s in dc.StatusUpdates
                                           where s.AccountId == accountId 
                                           select s);

      IEnumerable<T> obj = alerts.Union(updates);

      result = obj.ToList();
   }

   return result;
}

The problems I am having are:

1) I am dealing with two different types (Alerts and StatusUpdate) in my selects and
I am not sure how to combine them (or what type to return). I am guessing this might
be solved with generics?

2) In my SQL, I have this code: (SELECT 10 AS Expr1) AS AlertTypeId which adds the value ten to the temp column AlertTypeId (allowing the union to match it to Alert’s real column AlertTypeId). How are temp columns such as this accomplished in LINQ / how do I do this?

Thanks for your help.

EDIT———————————EDIT——————————————EDIT

OK, I am a little further along. Below is what I have currently. You will notice I added some logic to return the updates for friend relations. I also made this a generic method of type IList given that alerts and updates have to be generic to agree. I pass in StatusUpdate in the calling method (further down below).

public IList GetUpdatesByAccountId<T>(Int32 accountId)
{
    List<Friend> friends = _friendRepository.GetFriendsByAccountId(accountId);

    using (DataContext dc = _conn.GetContext())
    {
        // Get all the account ids related to this user
        var friendAccountIds =
            friends.Select(friend => friend.MyFriendsAccountId).Distinct();
            friendAccountIds = friendAccountIds.Concat(new[] { accountId });

        var updates =
            dc.StatusUpdates.Where(s => s.AccountId.HasValue && friendAccountIds.Contains(s.AccountId.Value)).Select(
                s => new { Alert = (Alert)null, StatusUpdate = s});

        var alerts =
            dc.Alerts.Where(a => a.AccountId == accountId).Select(
                a => new {Alert = a, StatusUpdate = (StatusUpdate) null});

        var obj = updates.Union(alerts).Take(100);

        return obj.OrderByDescending(su => su.StatusUpdate.CreateDate).ToList();

    }

}

And, the calling method:

protected void LoadStatus()
{

    repStatusUpdates.DataSource = _statusRepository
        .GetUpdatesByAccountId<StatusUpdate>(_userSession.CurrentUser.AccountId);

    repStatusUpdates.DataBind();

}

AND here are the interfaces to the repositories I am using to access my Alert and StatusUpdate tables via LINQ:

public interface IAlertRepository
    {
        List<Alert> GetAlertsByAccountId(Int32 accountId);
        void SaveAlert(Alert alert);
        void DeleteAlert(Alert alert);
    }

public interface IStatusUpdateRepository
    {
        StatusUpdate GetStatusUpdateById(Int32 statusUpdateId);
        List<StatusUpdate> GetStatusUpdatesByAccountId(Int32 accountId);
        List<StatusUpdate> GetFriendStatusUpdatesByAccountId(Int32 accountId, Boolean addPassedInAccount);
        void SaveStatusUpdate(StatusUpdate statusUpdate);
        List<StatusUpdate> GetTopNStatusUpdatesByAccountId(Int32 accountId, Int32 number);
        List<StatusUpdate> GetTopNFriendStatusUpdatesByAccountId(Int32 accountId, Int32 number, Boolean addPassedInAccount);        
    }

Current Problems:

1) When I compile this code, I get this strange error:

Unable to cast object of type
‘System.Data.Linq.SqlClient.SqlNew’ to
type
‘System.Data.Linq.SqlClient.SqlValue’.

The only reading I can find on it is this link although there isn’t a clear solution there (at least that I can tell). However, if the above LINQ code does not look good to you, maybe whatever you suggest will cause this error to disappear.

2) The above code is still not accounting for this line from the original SQL:

(SELECT 10 AS Expr1) AS AlertTypeId

but this is minor.

Thanks again for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:13:18+00:00Added an answer on May 16, 2026 at 10:13 am

    Try this (i converted the StatusUpdate to an alert, if this isn’t acceptable, you’re going to have to either convert the Alert to a StatusUpdate, or create a new class):

    var alerts = (from a in dc.Alerts
                  where a.AccountId == accountId
                  select a);
    var updates = (from s in dc.StatusUpdates
                   where s.AccountId == accountId 
                   select s)
                  .OrderByDescending( x => x.CreateDate)
                  .Take(100)
                  .Select( x => new Alert 
                    {
                       Message = x.Percent.ToString(),
                       CreateDate = x.CreateDate, 
                       AccountId = x.AccountId, 
                       AlertTypeId = 10 // Is this right?
                     }
                   );
    
     var obj = alerts.Union(updates);
    
     result = obj.ToList();
    

    The reason I do the Select last is so that you don’t have to construct a new alert for all the results your are not using.

    This will give you a list of Alerts.

    Using a generic in this situation is sort of hard to pull off. For instance, you can’t do this:

    IQueryable alerts = (from a in _alerts
    where a.AccountId == accountId
    select a);

    Because that implicitly converts a to type T. Even if you try to limit what T implements or inherits from:

    public List<T> GetSomething<T>(Int32 accountId) where T : IAlert// Interface that both StatusUpdates and IAlert implement
    public List<T> GetSomething<T>(Int32 accountId) where T : Alert
    public List<T> GetSomething<T>(Int32 accountId) where T : AlertBase // Base class for both Status and Alert
    

    You’ll still run into problems because there is no way to statically know exactly what type T is, so you cannot know if it can be converted from Alert and StatusUpdate.

    An alternative is to explicitly use IAlert as your return type:

    public List<IAlert> GetSomething(Int32 accountId)
    

    With IAlert:

    public interface IAlert
    {
        int AccountId { get; set; }
        int AlertTypeId { get; set; }
        DateTime CreateDate { get; set; }
        string Message { get; set; }
    }
    

    If you have have both Alert and StatusUpdate implement IAlert, you could rewrite it as so:

    IQueryable<IAlert> alerts = (from a in dc.Alerts
                  where a.AccountId == accountId
                  select a);
    IQueryable<IAlert> updates = (from s in dc.StatusUpdates
                   where s.AccountId == accountId 
                   select s)
                  .OrderByDescending( x => x.CreateDate)
                  .Take(100);
    
     var obj = alerts.Union(updates);
    
     result = obj.ToList();
    

    This is the route I would take instead of passing in some unknown type and trying to limit what it implements or inherits, because casting to that type might still be invalid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.